jsfiddle链接:http://jsfiddle.net/vN6fn/1/
假设我有这两个对象:
var obj1 = { data: [
{id:1, comment:"comment1"},
{id:2, comment:"comment2"},
{id:3, comment:"comment3"}
] }
var obj2 = { data: [
{id:2, comment:"comment2"},
{id:3, comment:"comment3"},
{id:4, comment:"comment4"}
] }
最终对象应该长这样:
var final = { data: [
{id:1, comment:"comment1"},
{id:2, comment:"comment2"},
{id:3, comment:"comment3"},
{id:4, comment:"comment4"}
] }
以下是需要考虑的一些事情:
- obj1和obj2可能有重复项
$.extend()
会替换对象,而$.merge()
不会删除重复项(我知道可以用for循环来解决,但我正在寻找更好的方法)。