0

我有 2 个 json 对象,每个对象都有我想加入的唯一 ID。

对象是:

main:

{"meta": {"limit": 100, "next": null, "offset": 0, "previous": null, "total_count": 1}, 
   "objects": [{"name": "John", "date": "2013-02-25", "mid": "512bd2f0e1cb890007b0cbf1"}]}


mid

{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, 
   "objects": [{"id": "512bd2f0e1cb890007b0cbf1", "title": "My Fav"}]}

您如何将它们与 jquery 加入 1 个对象并获取相同的 id 是

    where main.objects.mid = mid.objects.id

优选连接对象

    newobject.
{"meta": {"limit": 100, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"name": "John", "date": "2013-02-25", "mid": "512bd2f0e1cb890007b0cbf1"
            "mid": {title": "My Fav"}}
}]}

如果您注意到,只需将数据中间对象附加到主标题中。

4

4 回答 4

0

你可以使用 jQuery.extend,这里是 url:

http://api.jquery.com/jQuery.extend/

于 2013-03-01T14:15:03.703 回答
0
a = {"meta": {"limit": 100, "next": null, "offset": 0, "previous": null, "total_count": 1}, 
   "objects": [{"name": "John", "date": "2013-02-25", "mid": "512bd2f0e1cb890007b0cbf1"}]}

b = {"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, 
   "objects": [{"id": "512bd2f0e1cb890007b0cbf1", "title": "My Fav"}]}

console.log($.extend({},a,b));

这会将目标设置为新对象,以确保在不修改原始对象的情况下进行深层复制。

于 2013-03-01T14:15:45.783 回答
0

我认为扩展是你想要的。 http://api.jquery.com/jQuery.extend/

于 2013-03-01T14:14:27.543 回答
0

很难说没有更好的描述,但我会使用合并它们$.extend()

var object1 = {"meta": {"limit": 100} };

var object2 = {"meta": {"limit": 50} };

var mergedObjects = $.extend(object1, object2);

API

于 2013-03-01T14:14:45.643 回答