71

我有两个 json 数组,比如

var json1 = [{id:1, name: 'xxx' ...}]
var json2 = [{id:2, name: 'xyz' ...}]

我希望它们合并到单个数组中

var finalObj = [{id:1, name: 'xxx' ...},{id:2, name: 'xyz' ...}]
4

10 回答 10

121

你想要的concat方法。

var finalObj = json1.concat(json2);
于 2012-04-30T13:55:08.787 回答
37

在第一次出现时,“合并”这个词会让人认为您需要使用.extend,这是“合并”JSON 对象的正确 jQuery 方式。但是,$.extend(true, {}, json1, json2);将导致共享相同键名的所有值被参数中提供的最新值覆盖。正如对您问题的审查所示,这是不希望的。

您要寻找的是一个简单的 javascript 函数,称为.concat。这将像:

var finalObj = json1.concat(json2);

虽然这不是原生 jQuery 函数,但您可以轻松地将其添加到 jQuery 库中以供将来使用,如下所示:

;(function($) {
    if (!$.concat) {
        $.extend({
            concat: function() {
                return Array.prototype.concat.apply([], arguments);
            }
        });
    }
})(jQuery);

然后根据需要回忆它,例如:

var finalObj = $.concat(json1, json2);

您还可以将它用于这种类型的多个数组对象,例如:

var finalObj = $.concat(json1, json2, json3, json4, json5, ....);

如果你真的想要它 jQuery 风格并且非常简短和甜美(又名缩小)

;(function(a){a.concat||a.extend({concat:function(){return Array.prototype.concat.apply([],arguments);}})})(jQuery);

;(function($){$.concat||$.extend({concat:function(){return Array.prototype.concat.apply([],arguments);}})})(jQuery);

$(function() {
    var json1 = [{id:1, name: 'xxx'}],
        json2 = [{id:2, name: 'xyz'}],
        json3 = [{id:3, name: 'xyy'}],
        json4 = [{id:4, name: 'xzy'}],
        json5 = [{id:5, name: 'zxy'}];
    
    console.log(Array(10).join('-')+'(json1, json2, json3)'+Array(10).join('-'));
    console.log($.concat(json1, json2, json3));
    console.log(Array(10).join('-')+'(json1, json2, json3, json4, json5)'+Array(10).join('-'));
    console.log($.concat(json1, json2, json3, json4, json5));
    console.log(Array(10).join('-')+'(json4, json1, json2, json5)'+Array(10).join('-'));
    console.log($.concat(json4, json1, json2, json5));
});
center { padding: 3em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<center>See Console Log</center>

jsFiddle

于 2012-04-30T13:54:31.957 回答
22

你可以试试合并

var finalObj = $.merge(json1, json2);
于 2012-04-30T13:55:54.010 回答
6

由于您使用的是 jQuery。jQuery.extend() 方法怎么样?

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

描述:将两个或多个对象的内容合并到第一个对象中。

于 2012-04-30T13:54:55.090 回答
4

你可以使用 Es 6 的新特性来做到这一点:

var json1 = [{id:1, name: 'xxx' , ocupation : 'Doctor' }];

var json2 = [{id:2, name: 'xyz' ,ocupation : 'SE'}];

var combineJsonArray = [...json1 , ...json2];

//output should like this [ { id: 1, name: 'xxx', ocupation: 'Doctor' },
  { id: 2, name: 'xyz', ocupation: 'SE' } ]

或者您可以在两个 json 数组之间放置额外的字符串或任何内容:

var json3 = [...json1 ,"test", ...json2];

// output should like this : [ { id: 1, name: 'xxx', ocupation: 'Doctor' },
  'test',
  { id: 2, name: 'xyz', ocupation: 'SE' } ]
于 2017-10-24T10:09:42.073 回答
1

也许,您可以使用 javascript 的数组语法:

var finalObj =[json1 , json2]
于 2014-02-27T10:54:19.137 回答
0

如果使用 es6,您可以使用新的 js 扩展运算符:

var json1 = [{id:1, name: 'xxx'}]
var json2 = [{id:2, name: 'xyz'}]
var finalObj = [...json1, ...json2]

console.log(finalObj)

于 2018-09-04T06:25:11.940 回答
0

您可以使用Lodash _.merge函数来实现这一点。

var json1 = [{id:1, name: 'xxx'}];
var json2 = [{id:2, name: 'xyz'}];
var merged = _.merge(_.keyBy(json1, 'id'), _.keyBy(json2, 'id'));
var finalObj = _.values(merged);

console.log(finalObj);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>

于 2018-09-04T06:07:54.423 回答
-1

试试下面的代码,使用 jQuery 扩展方法:

var json1 = {"name":"ramesh","age":"12"};

var json2 = {"member":"true"};

document.write(JSON.stringify($.extend(true,{},json1,json2)))
于 2016-07-14T08:49:39.650 回答
-3
var json1=["Chennai","Bangalore"];
var json2=["TamilNadu","Karanataka"];

finaljson=json1.concat(json2);
于 2017-03-07T11:59:50.803 回答