Add key value pair to all objects in array

后端 未结 10 1889
醉酒成梦
醉酒成梦 2020-12-04 10:43

I wanted to add a key:value parameter to all the objects in an array.

eg:

var arrOfObj = [{name: \'eve\'},{name:\'john\'},{name:\'jane\'}];
<         


        
10条回答
  •  离开以前
    2020-12-04 11:17

    Looping through the array and inserting a key, value pair is about your best solution. You could use the 'map' function but it is just a matter of preference.

    var arrOfObj = [{name: 'eve'},{name:'john'},{name:'jane'}];
    arrOfObj.map(function (obj) { 
       obj.isActive = true;
    });
    

提交回复
热议问题