samedi 7 mars 2015

How to add an extra element in every nested array of a JSON object?

I have a JSON object like this:



[
{ poster_ip: '127.0.0.1',
post_body: 'example',
__v: 0 },

{ poster_ip: '127.0.0.1',
post_body: 'example',
__v: 0 }
]


how can i add an extra element to every nested array so it would be like this:



[
{ poster_ip: '127.0.0.1',
post_body: 'example',
__v: 0,
newName: NewValue },

{ poster_ip: '127.0.0.1',
post_body: 'example',
__v: 0,
newName: NewValue },
]


I tried this but it didn't work:



for(var i=0; i<JSONobject.length; i++){
JSONobject[i].newName= NewValue;
}


I'm using node.js, the JSON object is the result i get from a mongodb query.


Aucun commentaire:

Enregistrer un commentaire