2

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.

5
  • 2
    Seem to work fine: jsfiddle.net/AbdiasSoftware/fPJ2u (?) Tested in FF and Chrome. No id in my console. Commented Jul 1, 2013 at 9:59
  • It seems to be working fine in firebug. Commented Jul 1, 2013 at 10:01
  • @Ken-AbdiasSoftware It's not in client side, but in server side with node v0.8. Commented Jul 1, 2013 at 10:02
  • 3
    I tested your exact code above in node v0.8.14 and I'm seeing the correct behaviour, i.e. the id element is gone. Apologies if I'm asking the obvious, but is the code above the code that you're actually running? Commented Jul 1, 2013 at 10:46
  • I've just tried your code on the Node 0.10.12 command line, and I get your required output from console.log(array), i.e. [{ name: 'test' }, { name: 'test2' }]. I'd be surprised if the native array object's behaviour had been changed between these two versions. Commented Jul 1, 2013 at 11:04

3 Answers 3

2

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:

screenshot
(source: nicon.nl)

Sign up to request clarification or add additional context in comments.

5 Comments

I am using this in node, where id is not deleting.
See the screenshot of node output.
What does it means? I didn't get u... :)
It is your code followed by a loop to check the properties of the first object of the array variable ran through the node.exe interpreter. The top shows the code, the bottom half the result. It only shows test, which is the value of the only (own) property of object contained by array[0], so the id property is removed.
Thanks, you are right. I don't know how it happens to be returning id after deleting it. I just started with fresh copy and it does work great today...
0

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.

1 Comment

I am using this in server with node v0.8, where id is not deleting.
0

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;

1 Comment

I am looking for solution without using a external temp Array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.