If I have this object:
[
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
And I want this:
[
{ "firstName":"John" , "lastName":"Doe", "status":"Normal" },
{ "firstName":"Anna" , "lastName":"Smith", "status":"Normal" },
{ "firstName":"Peter" , "lastName":"Jones", "status":"Normal" }
]
What’s the easiest way to do it in Javascript ? Is there an one-liner way using one of the common libraries ? I know jQuery.each(function...) will work, but I’m thinking there might be a better way. I’m also interested in the reverse, changing the second one to the first (removing the ‘status’ property).
Second one:
Array.forEach is similar to Array.map, but it doesn’t create a new array. You can also use this in older browsers by including the .forEach implementation provided on https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach