Kaan Karaca Kaan Karaca - 5 months ago 10
Javascript Question

How to get largest value in multi-dimensional array

function largestOfFour(arr) {
var max = 0;
var newArr = [];

for (var i = 0; i < arr.length; i++) {
for (var j = i; j < arr.length; j++) {
max = Math.max(max, arr[i][j]);
}
newArr.push(max);
}
return newArr;
}


Here is my code. It works for me but I want to know is there any other sort way to do this?

Answer

Try this:

function largestOfFour(arr) {
  return arr.map(function(item){
    return Math.max.apply(null, item);
  });
}