如何在jquery中合并两个数组对象元素

时间:2017-04-12 11:01:54

标签: javascript jquery arrays object merge

输入:

var b= [{
    "cat_id": "1",
    "cat_name": "teaching"
  }];

  var a= [  {
    "username": "r",
    "password": "r"
  }];

我想要这样的输出

[{"username":"r","password":"r","cat_id":"1","cat_name":"teaching"}]

3 个答案:

答案 0 :(得分:2)

您可以使用Object.assign()扩展语法。



var b = [{"cat_id": "1","cat_name": "teaching"}];
var a = [{"username": "r","password": "r"}];

var result = [Object.assign({}, ...a, ...b)]
console.log(result)




答案 1 :(得分:1)

使用Object.assign的可能解决方案。



var b = [{"cat_id":"1","cat_name":"teaching"}], a = [{"username":"r","password":"r"}],
    res2 = [Object.assign({}, ...a.concat(b))];
  
    console.log(res2);




答案 2 :(得分:0)

您可以使用扩展功能合并对象

$.extend( a, b);