如何使用jquery extend扩展json对象?

时间:2010-11-02 14:53:35

标签: jquery json object extend

var dataset = {"read_data":[{"date":"2010\/11\/02 03:30:05","value":"2"},
                                {"date":"2010\/11\/02 03:30:06","value":"1"}]};

var append = {"read_data":[{"date":"2010\/11\/02 03:30:07","value":"3"},
                           {"date":"2010\/11\/02 03:30:08","value":"4"}]};

我有两个这样的数据对象。如何使用JQuery.extend();将它们合并在一起?所以他们看起来像这样:

dataset === {"read_data":[{"date":"2010\/11\/02 03:30:05","value":"2"},
                            {"date":"2010\/11\/02 03:30:06","value":"1"},
                            {"date":"2010\/11\/02 03:30:07","value":"3"},
                            {"date":"2010\/11\/02 03:30:08","value":"4"}]};

本网站包含的所有方法根本不起作用。

1 个答案:

答案 0 :(得分:7)

jQuery.extend会覆盖具有相同名称的成员,并且不支持数组合并。你需要这样做:

$.merge(dataset.read_data, append.read_data);