Home > Back-end >  removing a column from an array in javascript
removing a column from an array in javascript

Time:01-30

I have a multidimentional array in javascript like this

Arr[i].ProductId
Arr[i].ProductName
Arr[i].ProductCode
Arr[i].ProductDescription
Arr[i].ProductOrigin
Arr[i].ProductInfo

Arr is populated with data when everything is done with Arr,I want to have another array without (ProductCode,ProductOrigin,ProductInfo) columns.

I have done this

var list2 = JSON.parse(JSON.stringify(Arr));
for (i in list2) {
    delete list2[i].ProductCode;
    delete list2[i].ProductOrigin;
    delete list2[i].ProductInfo;
}

this is iterating through the array. is it possible to remove the Columns without iterating? or what is the better solution for that?

CodePudding user response:

You need to iterate over the list to get the new version of every element. Another way to do this is using Array#map:

const list2 = Arr.map(({ ProductCode, ProductOrigin, ProductInfo, ...e }) => e);
  •  Tags:  
  • Related