如何在Javascript中将两个数组添加到一个数组中[重复]

时间:2021-07-26 12:21:00

This question already has an answer here:

这个问题在这里已有答案:

I am trying to add a couple of arrays into one array in Javascript. It seems my way is not good enough. Let's suppose we have these three arrays as below:

我试图在Javascript中将一些数组添加到一个数组中。看来我的方式还不够好。我们假设我们有以下三个数组:

var array_1 = [{"a" : 1, "b" : 2, "c" : 3}];
var array_2 = [{"d" : 4, "e" : 5, "f" : 6}];
var array_3 = new Array();

I would like to add array_1 and then array_2 into array_3 respectively. For instance, I want to make sure that array_1 is fully added first and then array_2 as below:

我想分别将array_1和array_2添加到array_3中。例如,我想确保先完全添加array_1,然后再按以下方式添加array_2:

{"a" : 1, "b" = 2, "c" = 3"}
{"d" : 4, "e" = 5, "f" = 6"}

Could anyone please help me with this piece of code. Your help would be very much appreciated.

任何人都可以帮我解决这段代码。非常感谢您的帮助。

3 个解决方案

#1


1  

var array_3 = array_1.concat(array_2);

#2


1  

The way that you have your arrays set up is very strange. What you have are 2 arrays with 1 element in them each, and that element is an Object with 3 properties. The output that you listed is actually invalid javascript or JSON syntax. But, I can get you close to what you want, I think...

您设置阵列的方式非常奇怪。你拥有的是2个数组,每个数组中包含1个元素,该元素是具有3个属性的Object。您列出的输出实际上是无效的javascript或JSON语法。但是,我认为,我可以让你接近你想要的东西......

var array_1 = [{"a" : 1, "b" : 2, "c" : 3}];
var array_2 = [{"d" : 4, "e" : 5, "f" : 6}];
var array_3 = new Array();

function merge_objects(obj1,obj2){
    var obj3 = {};
    for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
    for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
    return obj3;
}

array_3[0] = merge_objects(array_1[0], array_2[0]);

#3


0  

What you're asking for is Array.prototype.concat which combines two arrays (or more) into a new array. All you need is:

你要求的是Array.prototype.concat,它将两个数组(或更多)组合成一个新数组。所有你需要的是:

var array_3 = array_1.concat(array_2);

#1


1  

var array_3 = array_1.concat(array_2);

#2


1  

The way that you have your arrays set up is very strange. What you have are 2 arrays with 1 element in them each, and that element is an Object with 3 properties. The output that you listed is actually invalid javascript or JSON syntax. But, I can get you close to what you want, I think...

您设置阵列的方式非常奇怪。你拥有的是2个数组,每个数组中包含1个元素,该元素是具有3个属性的Object。您列出的输出实际上是无效的javascript或JSON语法。但是,我认为,我可以让你接近你想要的东西......

var array_1 = [{"a" : 1, "b" : 2, "c" : 3}];
var array_2 = [{"d" : 4, "e" : 5, "f" : 6}];
var array_3 = new Array();

function merge_objects(obj1,obj2){
    var obj3 = {};
    for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
    for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
    return obj3;
}

array_3[0] = merge_objects(array_1[0], array_2[0]);

#3


0  

What you're asking for is Array.prototype.concat which combines two arrays (or more) into a new array. All you need is:

你要求的是Array.prototype.concat,它将两个数组(或更多)组合成一个新数组。所有你需要的是:

var array_3 = array_1.concat(array_2);