如何将对象添加到每个对象数组

时间:2022-08-13 07:07:21

i have array of objects like this

我有像这样的对象数组

var data=[{test:"a1"},{test:"b1"}];

need to add the one more object inside the object output should be like this

需要在对象输出中添加一个以上的对象应该是这样的

[  
   {  
      "test":"a1",
      "system":{  
         "tenentid":"xxxxx",
         "createdByUser":{  
            "lastName":"x",
            "firstName":"ABC"
         },
         "isdeleted":true
      }
   },
   {  
      "test":"b1",
      "system":{  
         "tenentid":"xxxxx",
         "createdByUser":{  
            "lastName":"x",
            "firstName":"ABC"
         },
         "isdeleted":true
      }
   }
]

plese can any one help me on this.

任何人都可以帮助我。

3 个解决方案

#1


2  

Iterate the array, add the property:

迭代数组,添加属性:

for (var i = 0; i < data.length; i++) {
    data[i].system = {}; //your new object here
}

#2


0  

Let's do it like we're writing node.js shall we?

我们就像编写node.js一样吗?

data.forEach(function(someData){
    someData.system={};
});

#3


-2  

An easier way of doing it is:

更简单的方法是:

for(var d of data) {
   d.system = {}; // Insert object here
}

#1


2  

Iterate the array, add the property:

迭代数组,添加属性:

for (var i = 0; i < data.length; i++) {
    data[i].system = {}; //your new object here
}

#2


0  

Let's do it like we're writing node.js shall we?

我们就像编写node.js一样吗?

data.forEach(function(someData){
    someData.system={};
});

#3


-2  

An easier way of doing it is:

更简单的方法是:

for(var d of data) {
   d.system = {}; // Insert object here
}