如何在Javascript中合并两个不同的数组?

时间:2022-10-31 12:14:11

I have this array,

我有这个数组,

var arr1 = [19, 1, 1, 1, 1];

and another array,

和另一个数组,

var arr2 = ["Default", "Dynamic", "Assessment", "Risk", "Privacy"];

What I would like is to merge them somehow such that it would look something like this,

我想要的是合并它们使它看起来像这样,

var merged_array = [ ["Default", 19], ["Dynamic", 1], ["Assessment", 1], ["Risk", 3], ["Privacy", 2] ];

how would I do that? I tried join but I did not achieve what I wanted. Thanks in advance!

我该怎么做呢?我试着加入,但没有达到我想要的。提前谢谢!

5 个解决方案

#1


1  

Using a for loop:

使用一个for循环:

var merged_array = new Array(arr1.length);
for (var i = 0; i < arr1.length; i++) {
  merged_array[i] = new Array(arr2[i], arr1[i]);
}

This assumes arr1 and arr2 have the same length.

假设arr1和arr2的长度相同。

#2


1  

If you have jquery or equivalent:

如果你有jquery或类似的:

var merged_array = $.map(arr1, function(e, i) {
    return [arr2[i], e];
});

If not, then just use a for loop, same idea.

如果不是,那么就使用for循环。

var merged_array = []
for (var i = 0; i < arr1.length && i < arr2.length; i++) {
    merged_array[i] = [arr2[i], arr1[i]];
}

#3


1  

var result = new Array();
for (var i=0; i<arr1 .length && i<arr2.length ; i++) {
    result[i] = new Array();
    result[i][0] = arr1[i];
    result[i][1] = arr2[i];
}

#4


1  

I'd probably extend the array object especially if this is a common task. Something like this:

我可能会扩展数组对象,特别是如果这是一个常见的任务。是这样的:

Array.prototype.myMerge = function(arr){
    var returnArr = [];
    for(var i = 0, len = this.length; i < len; i++){
        returnArr[i] = [this[i], arr[i]];
    }
    return returnArr;
};

then you could call it like this:

然后你可以这样称呼它:

var merged_array = arr1.myMerge(arr2)

Of course you'd have to add some error checking on the length of the arrays, this function assumes that arr1 is longer or the same length as arr2. But that depends on what you want to do if arr1 and arr2 are different lengths, your question seems to assume they are the same length.

当然,你需要对数组的长度添加一些错误检查,这个函数假设arr1比arr2长或者和arr2一样长。但这取决于你想做什么如果arr1和arr2的长度不同,你的问题似乎是假设它们的长度相同。

#5


1  

If you include the library underscore.js (prototype.js also has something similar), you can use _.zip(array1,array2). They provide example usage with 3 arrays.

如果包含库下划线。js(原型。js也有类似的东西),您可以使用_.zip(array1,array2)。它们提供了3个数组的示例用法。

From http://underscorejs.org/#zip :

从http://underscorejs.org/邮政编码:

zip_.zip(*arrays) Merges together the values of each of the arrays with the values at the corresponding position. Useful when you have separate data sources that are coordinated through matching array indexes. If you're working with a matrix of nested arrays, zip.apply can transpose the matrix in a similar fashion.

zip(*数组)将每个数组的值与相应位置的值合并在一起。当您有独立的数据源时,可以通过匹配的数组索引进行协调。如果使用嵌套数组的矩阵,请zip。apply可以以类似的方式转换矩阵。

_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
=> [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]

#1


1  

Using a for loop:

使用一个for循环:

var merged_array = new Array(arr1.length);
for (var i = 0; i < arr1.length; i++) {
  merged_array[i] = new Array(arr2[i], arr1[i]);
}

This assumes arr1 and arr2 have the same length.

假设arr1和arr2的长度相同。

#2


1  

If you have jquery or equivalent:

如果你有jquery或类似的:

var merged_array = $.map(arr1, function(e, i) {
    return [arr2[i], e];
});

If not, then just use a for loop, same idea.

如果不是,那么就使用for循环。

var merged_array = []
for (var i = 0; i < arr1.length && i < arr2.length; i++) {
    merged_array[i] = [arr2[i], arr1[i]];
}

#3


1  

var result = new Array();
for (var i=0; i<arr1 .length && i<arr2.length ; i++) {
    result[i] = new Array();
    result[i][0] = arr1[i];
    result[i][1] = arr2[i];
}

#4


1  

I'd probably extend the array object especially if this is a common task. Something like this:

我可能会扩展数组对象,特别是如果这是一个常见的任务。是这样的:

Array.prototype.myMerge = function(arr){
    var returnArr = [];
    for(var i = 0, len = this.length; i < len; i++){
        returnArr[i] = [this[i], arr[i]];
    }
    return returnArr;
};

then you could call it like this:

然后你可以这样称呼它:

var merged_array = arr1.myMerge(arr2)

Of course you'd have to add some error checking on the length of the arrays, this function assumes that arr1 is longer or the same length as arr2. But that depends on what you want to do if arr1 and arr2 are different lengths, your question seems to assume they are the same length.

当然,你需要对数组的长度添加一些错误检查,这个函数假设arr1比arr2长或者和arr2一样长。但这取决于你想做什么如果arr1和arr2的长度不同,你的问题似乎是假设它们的长度相同。

#5


1  

If you include the library underscore.js (prototype.js also has something similar), you can use _.zip(array1,array2). They provide example usage with 3 arrays.

如果包含库下划线。js(原型。js也有类似的东西),您可以使用_.zip(array1,array2)。它们提供了3个数组的示例用法。

From http://underscorejs.org/#zip :

从http://underscorejs.org/邮政编码:

zip_.zip(*arrays) Merges together the values of each of the arrays with the values at the corresponding position. Useful when you have separate data sources that are coordinated through matching array indexes. If you're working with a matrix of nested arrays, zip.apply can transpose the matrix in a similar fashion.

zip(*数组)将每个数组的值与相应位置的值合并在一起。当您有独立的数据源时,可以通过匹配的数组索引进行协调。如果使用嵌套数组的矩阵,请zip。apply可以以类似的方式转换矩阵。

_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
=> [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]