-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
  • 9,424
  • 17
  • 55
  • 78

3 Answers3

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);
LellisMoon
  • 4,810
  • 2
  • 12
  • 24
2

You can do like this

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

});

DEMO

brk
  • 48,835
  • 10
  • 56
  • 78
2

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