2

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:

[900, 332, 432, 145]

What would be the simplest way of accomplishing this?

Please no jQuery, just plain vanilla JS.

2
  • Did you try something yourself yet? Commented Mar 27, 2016 at 9:58
  • Combine your 4 arrays with array concat and sort them descending with array sort and take the first 4 for your new array. Commented Mar 27, 2016 at 10:02

5 Answers 5

4

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

var arr5 = [];
for (var i = 0; i < arr1.length; i++) {
    arr5.push(Math.max(arr1[i], arr2[i], arr3[i], arr4[i]));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Simple and elegant. Exactly what I was looking for.
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)

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

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.

Hope it helps.

Comments

0

This is a proposal with two nested Array#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>');

Comments

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);

Comments

0

I think that you should use madox2's solution!

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);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.