简体   繁体   English

从 javascript 中的数组中删除一列

[英]removing a column from an array in javascript

I have a multidimentional array in javascript like this我在 javascript 中有一个像这样的多维数组

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.使用 Arr 完成所有操作后,Arr 会填充数据,我想要另一个没有 (ProductCode,ProductOrigin,ProductInfo) 列的数组。

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?或者什么是更好的解决方案?

You need to iterate over the list to get the new version of every element.您需要遍历列表以获取每个元素的新版本。 Another way to do this is using Array#map :另一种方法是使用Array#map

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM