组合两个Javascript对象
我有两个目标组合两个Javascript对象,javascript,jquery,Javascript,Jquery,我有两个目标 var data = {property: [ {id: "1", name: "Snatch", type: "crime"}, {id: "2", name: "Witches of Eastwick", type: "comedy"}, {id: "3", name: "X-Men", type: "action"}, {id: "4", name: "Ordinary People", type: "drama"}, {id: "5", name: "B
var data = {property: [
{id: "1", name: "Snatch", type: "crime"},
{id: "2", name: "Witches of Eastwick", type: "comedy"},
{id: "3", name: "X-Men", type: "action"},
{id: "4", name: "Ordinary People", type: "drama"},
{id: "5", name: "Billy Elliot", type: "drama"},
{id: "6", name: "Toy Story", type: "children"}
]};
及
我可以使用jquery或javascript将两者结合起来吗
var data = {property: [
{id: "1", name: "Snatch", type: "crime"},
{id: "2", name: "Witches of Eastwick", type: "comedy"},
{id: "3", name: "X-Men", type: "action"},
{id: "4", name: "Ordinary People", type: "drama"},
{id: "5", name: "Billy Elliot", type: "drama"},
{id: "6", name: "Toy Story", type: "children"},
{id: "7", name: "Snatch", type: "crime"},
{id: "8", name: "Witches of Eastwick", type: "comedy"}
]};
我尝试过使用推送功能,但没有成功如何:
var data = {property: []}
var data2 = {property: []}
data.property =data.property.concat(data2.property);
使用阵列的
concat
功能,您可以加入它们。concat是一种解决方案,但也会处理任何重复项
var data3 = _.union(data.property, data2.property)
这里根本没有JSON。您有JavaScript对象……而您正试图组合的是数组。
var data3 = _.union(data.property, data2.property)