将两个json/javascript数组合并到一个数组中

时间:2022-01-29 12:13:08

I have two json arrays like

我有两个json数组

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

I want them merge in to single arrays

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

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

Regards

问候

8 个解决方案

#1


102  

You want the concat method.

你需要的是concat方法。

var finalObj = json1.concat(json2);

#2


36  

Upon first appearance, the word "merg" leads one to think you need to use .extend, which is the proper jQuery way to "merge" JSON objects. However, $.extend(true, {}, json1, json2); will cause all values sharing the same key name to be overridden by the latest supplied in the params. As review of your question shows, this is undesired.

乍一看,“merg”这个词会让人觉得需要使用.extend,这是“合并”JSON对象的合适jQuery方法。然而,美元。扩展(真的,{ },json1 json2);将导致共享相同密钥名的所有值被params中提供的最新值覆盖。回顾你的问题,这是不可取的。

What you seek is a simple javascript function known as .concat. Which would work like:

您需要的是一个简单的javascript函数。concat。这将工作:

var finalObj = json1.concat(json2);

While this is not a native jQuery function, you could easily add it to the jQuery library for simple future use as follows:

虽然这不是一个本机jQuery函数,但您可以轻松地将其添加到jQuery库中,以便将来简单地使用它:

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

And then recall it as desired like:

然后按照要求回忆:

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

You can also use it for multiple array objects of this type with a like:

您也可以将它用于此类的多个数组对象,例如:

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

And if you really want it jQuery style and very short and sweet (aka minified)

如果你真的想要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

jsFiddle

#3


21  

You could try merge

你可以尝试合并

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

#4


5  

Since you are using jQuery. How about the jQuery.extend() method?

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

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

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

Description: Merge the contents of two or more objects together into the first object.

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

#5


2  

Maybe, you can use the array syntax of javascript :

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

var finalObj =[json1 , json2]

#6


2  

You can do this using Es 6 new feature :

你可以使用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' } ]

Or You can put extra string or anything between two json array :

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

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

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

#7


-1  

Try the code below, using jQuery extend method:

尝试以下代码,使用jQuery扩展方法:

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

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

document.write(JSON.stringify($.extend(true,{},json1,json2)))

#8


-2  

var json1=["Chennai","Bangalore"];
var json2=["TamilNadu","Karanataka"];

finaljson=json1.concat(json2);

#1


102  

You want the concat method.

你需要的是concat方法。

var finalObj = json1.concat(json2);

#2


36  

Upon first appearance, the word "merg" leads one to think you need to use .extend, which is the proper jQuery way to "merge" JSON objects. However, $.extend(true, {}, json1, json2); will cause all values sharing the same key name to be overridden by the latest supplied in the params. As review of your question shows, this is undesired.

乍一看,“merg”这个词会让人觉得需要使用.extend,这是“合并”JSON对象的合适jQuery方法。然而,美元。扩展(真的,{ },json1 json2);将导致共享相同密钥名的所有值被params中提供的最新值覆盖。回顾你的问题,这是不可取的。

What you seek is a simple javascript function known as .concat. Which would work like:

您需要的是一个简单的javascript函数。concat。这将工作:

var finalObj = json1.concat(json2);

While this is not a native jQuery function, you could easily add it to the jQuery library for simple future use as follows:

虽然这不是一个本机jQuery函数,但您可以轻松地将其添加到jQuery库中,以便将来简单地使用它:

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

And then recall it as desired like:

然后按照要求回忆:

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

You can also use it for multiple array objects of this type with a like:

您也可以将它用于此类的多个数组对象,例如:

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

And if you really want it jQuery style and very short and sweet (aka minified)

如果你真的想要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

jsFiddle

#3


21  

You could try merge

你可以尝试合并

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

#4


5  

Since you are using jQuery. How about the jQuery.extend() method?

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

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

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

Description: Merge the contents of two or more objects together into the first object.

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

#5


2  

Maybe, you can use the array syntax of javascript :

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

var finalObj =[json1 , json2]

#6


2  

You can do this using Es 6 new feature :

你可以使用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' } ]

Or You can put extra string or anything between two json array :

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

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

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

#7


-1  

Try the code below, using jQuery extend method:

尝试以下代码,使用jQuery扩展方法:

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

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

document.write(JSON.stringify($.extend(true,{},json1,json2)))

#8


-2  

var json1=["Chennai","Bangalore"];
var json2=["TamilNadu","Karanataka"];

finaljson=json1.concat(json2);