JavaScript:将多个数组的最高键值合并到一个数组中

时间:2022-06-22 12:55:27

Let's say I have four arrays:

假设我有四个数组:

var arr1 = [117, 121, 18, 24];
var arr2 = [132, 19, 432, 23];
var arr3 = [32, 23, 137, 145];
var arr4 = [900, 332, 23, 19];

I need to create a new array from these, arr5, where key 1 is the highest number from key 1 of arr1, arr2, arr3, arr4, and the same for key 2, 3, etc. So I would have arr5 as:

我需要从这些arr5中创建一个新的数组,其中键1是arr1 arr2 arr3 arr4 arr4的键1的最大值,同样的键2 3,等等。

[900, 332, 432, 145]

What would be the simplest way of accomplishing this?

最简单的方法是什么?

Please no jQuery, just plain vanilla JS.

请不要使用jQuery,只使用普通的JS。

5 个解决方案

#1


4  

Solution using simple for-loop and Math.max(). Assuming that your arrays have the same length:

使用简单的for循环和Math.max()的解决方案。假设您的数组具有相同的长度:

var arr5 = [];
for (var i = 0; i < arr1.length; i++) {
    arr5.push(Math.max(arr1[i], arr2[i], arr3[i], arr4[i]));
}

#2


0  

There are multiple ways of accomplishing this. One would be using Math.max (https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Math/max)

实现这一点有多种方法。一个是用数学。马克斯(https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Math/max)

While using the spread operator (https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Spread_operator) you can do it like this:

当使用扩展操作符(https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Spread_operator)时,您可以这样做:

arr5.push( Math.max(...arr1) );
arr5.push( Math.max(...arr2) );
// etc..

Maybe try yourself how to accomplish this with a dynamic input of arrays, maybe a little wrapping function for the Math.max call, where you pass all arrays you like and which returns an array with only the max values.

也许你可以尝试一下如何用数组的动态输入来实现这个目标,或者用一个小的包装函数来进行数学运算。max调用,传递所有你喜欢的数组,返回一个只有max值的数组。

Hope it helps.

希望它可以帮助。

#3


0  

This is a proposal with two nested Array#forEach.

这是一个包含两个嵌套数组#forEach的建议。

var arr1 = [117, 121, 18, 24],
    arr2 = [132, 19, 432, 23],
    arr3 = [32, 23, 137, 145],
    arr4 = [900, 332, 23, 19],
    result = function (array) {
        var r = [];
        array.forEach(function (a) {
            a.forEach(function (b, i) {
                if (!( r[i] > b)) {
                    r[i] = b;
                }
            });
        });
        return r;
    }([arr1, arr2, arr3, arr4]);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

#4


0  

have a look at this solution

看看这个解决方案

var arr1 = [117, 121, 18, 24];
var arr2 = [132, 19, 432, 23];
var arr3 = [32, 23, 137, 145];
var arr4 = [900, 332, 23, 19];
var arr5 = [];

Array.prototype.max = function() {
  return Math.max.apply(null, this);
};


for(var i=0; i<4; i++){
   var arr=[arr1[i], arr2[i], arr3[i], arr4[i]];
   var max = Math.max.apply(null, arr);
   arr5.push(max);

}

console.log(arr5);

#5


0  

I think that you should use madox2's solution!

我认为你应该使用madox2的解决方案!

That said – one way to tackle about your problem is to let your arrays form a matrix, transpose that matrix, and map Math.max over the resulting rows.

也就是说,解决这个问题的一种方法是让你的数组形成一个矩阵,转置那个矩阵,然后映射数学。最大除以结果行。

Something like this:

是这样的:

function transpose(m) {
    return m[0].map(function(_, i) {
        return m.map(function(arr) {
            return arr[i];
         });
    });
}
var arr5 = transpose([arr1, arr2, arr3, arr4]).map(function(e) {
    return Math.max.apply(null, e);
});

#1


4  

Solution using simple for-loop and Math.max(). Assuming that your arrays have the same length:

使用简单的for循环和Math.max()的解决方案。假设您的数组具有相同的长度:

var arr5 = [];
for (var i = 0; i < arr1.length; i++) {
    arr5.push(Math.max(arr1[i], arr2[i], arr3[i], arr4[i]));
}

#2


0  

There are multiple ways of accomplishing this. One would be using Math.max (https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Math/max)

实现这一点有多种方法。一个是用数学。马克斯(https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Math/max)

While using the spread operator (https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Spread_operator) you can do it like this:

当使用扩展操作符(https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/Spread_operator)时,您可以这样做:

arr5.push( Math.max(...arr1) );
arr5.push( Math.max(...arr2) );
// etc..

Maybe try yourself how to accomplish this with a dynamic input of arrays, maybe a little wrapping function for the Math.max call, where you pass all arrays you like and which returns an array with only the max values.

也许你可以尝试一下如何用数组的动态输入来实现这个目标,或者用一个小的包装函数来进行数学运算。max调用,传递所有你喜欢的数组,返回一个只有max值的数组。

Hope it helps.

希望它可以帮助。

#3


0  

This is a proposal with two nested Array#forEach.

这是一个包含两个嵌套数组#forEach的建议。

var arr1 = [117, 121, 18, 24],
    arr2 = [132, 19, 432, 23],
    arr3 = [32, 23, 137, 145],
    arr4 = [900, 332, 23, 19],
    result = function (array) {
        var r = [];
        array.forEach(function (a) {
            a.forEach(function (b, i) {
                if (!( r[i] > b)) {
                    r[i] = b;
                }
            });
        });
        return r;
    }([arr1, arr2, arr3, arr4]);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

#4


0  

have a look at this solution

看看这个解决方案

var arr1 = [117, 121, 18, 24];
var arr2 = [132, 19, 432, 23];
var arr3 = [32, 23, 137, 145];
var arr4 = [900, 332, 23, 19];
var arr5 = [];

Array.prototype.max = function() {
  return Math.max.apply(null, this);
};


for(var i=0; i<4; i++){
   var arr=[arr1[i], arr2[i], arr3[i], arr4[i]];
   var max = Math.max.apply(null, arr);
   arr5.push(max);

}

console.log(arr5);

#5


0  

I think that you should use madox2's solution!

我认为你应该使用madox2的解决方案!

That said – one way to tackle about your problem is to let your arrays form a matrix, transpose that matrix, and map Math.max over the resulting rows.

也就是说,解决这个问题的一种方法是让你的数组形成一个矩阵,转置那个矩阵,然后映射数学。最大除以结果行。

Something like this:

是这样的:

function transpose(m) {
    return m[0].map(function(_, i) {
        return m.map(function(arr) {
            return arr[i];
         });
    });
}
var arr5 = transpose([arr1, arr2, arr3, arr4]).map(function(e) {
    return Math.max.apply(null, e);
});