Here is my code
var array = [{ id: 1, name: 'test' }, { id: 2, name: 'test2' }];
I need above array to be changed like below
[{ name: 'test' }, { name: 'test2' }]
I tried with delete
array.forEach(function(arr, i) {
delete array[i].id;
});
console.log(array);
Outputs as
[ { id: 1, name: 'test' },
{ id: 2, name: 'test2'} ]
But it doesn't remove the id item. How to remove array object item?
I am using this in node v0.8.
The id property is deleted, as can be demonstrated by:
for (var l in array[0]) {
if (array[0].hasOwnProperty(l)) {
console.log(array[0][l]);
}
}
See jsFiddle
Screenshot of node.js output:
![]()
Hm, here is your code with jQuery 1.9.1 and it works good: 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);
By the way, you would like to delete 'property' Id from objects in array- that's better to understand you.
You have to parse the array and build the new version then replace it.
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;