Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

removing a column from an array in javascript

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading