-2

I have an Array of Objects like:

[
   {
      title: 'Title 1',
      value: 'value 1'
   },
   {
      title: 'Title 2',
      value: 'value 2'
   }
]

I need to add a new param to all the objects.

I know I can do a for over the array and modify each object, I'm trying to find a way to solve with a native method. Does that exists?

Pablo
  • 8,001
  • 13
  • 50
  • 74

4 Answers4

3

var arrayObj=[
   {
      title: 'Title 1',
      value: 'value 1'
   },
   {
      title: 'Title 2',
      value: 'value 2'
   }
];

arrayObj.forEach(function(obj){
  obj.newProp='new';
  });
console.log(arrayObj);
Mr.Bruno
  • 4,519
  • 2
  • 11
  • 24
2

You can do like this

a.forEach(function(item,index){
item.newParam=index

});

DEMO

brk
  • 46,805
  • 5
  • 49
  • 71
2

YourList.forEach(function(entry) { entry['newElement'] = "test"; console.log(entry); });

-3
var newData = data.map(function(o){
  o.newElem = 10;//whatever you want
  return o;
});
user2693928
  • 3,507
  • 2
  • 14
  • 25