Get largest value in multi-dimensional array javascript or coffeescript
You can use .reduce()
...
array.reduce(function(max, arr) {
return Math.max(max, arr[0]);
}, -Infinity)
Here's a version that doesn't use Math.max
...
array.reduce(function(max, arr) {
return max >= arr[0] ? max : arr[0];
}, -Infinity);
...and a jsPerf test.
http://jsfiddle.net/zerkms/HM7es/
var max = Math.max.apply(Math, arr.map(function(i) {
return i[0];
}));
So at first you use array.map()
to convert the 2-dimensional array to a flat one, and after that use Math.max()
A simple solution using Underscore.js' max
that avoids generating an intermediate array:
max = _(array).max(_.first)[0]
(JSFiddle)