这是我的代码
var array = [{ id: 1, name: 'test' }, { id: 2, name: 'test2' }];
我需要像下面这样更改上面的数组
[{ name: 'test' }, { name: 'test2' }]
我尝试删除
array.forEach(function(arr, i) {
delete array[i].id;
});
console.log(array);
输出为
[ { id: 1, name: 'test' },
{ id: 2, name: 'test2'} ]
但它不会删除 id
项。如何删除数组对象项?
我在 Node v0.8中使用它。
最佳答案
id
属性已被删除,如下所示:
for (var l in array[0]) {
if (array[0].hasOwnProperty(l)) {
console.log(array[0][l]);
}
}
参见jsFiddle
node.js
输出的屏幕截图:
(来源:nicon.nl)
关于javascript - 如何在javascript中删除数组对象项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17401657/