如何在javascript中将一个数组对象添加到另一个数组对象

时间:2018-02-14 05:54:59

标签: javascript

我的情景是:

var a=[{name:"Rambabu",id:"101"},{name:"Divya",id:"102"}];

var b=[{name:"manu",id:"103"},{name:"sudha",id:"104"}];

var c= a array + b array data objects.

预期产出:

c= [{name:"Rambabu",id:"101"},{name:"Divya",id:"102"},{name:"manu",id:"103"},{name:"sudha",id:"104"}]

1 个答案:

答案 0 :(得分:2)

使用.concat

var a=[{name:"Rambabu",id:"101"},{name:"Divya",id:"102"}];

var b=[{name:"manu",id:"103"},{name:"sudha",id:"104"}];

var c = a.concat(b);

console.log(c);

或者您也可以使用点差运算符...

var a=[{name:"Rambabu",id:"101"},{name:"Divya",id:"102"}];

var b=[{name:"manu",id:"103"},{name:"sudha",id:"104"}];

var c = [...a, ...b];

console.log(c);