I have an array that looks like the following:
array = [[1, 5], [4, 7], [3, 8], [2, 3],
[12, 4], [6, 6], [4, 1], [3, 2],
[8, 14]]
12
Math.max.apply Math, array
unless device.IE
justTheDates = magnitudeArray.map (i) -> i[0]
@earliest = Math.min.apply Math, justTheDates
@latest = Math.max.apply Math, justTheDates
else
@earliest = magnitudeArray[0][0]
@latest = magnitudeArray[0][0]
for magnitudeItem in magnitudeArray
@earliest = magnitudeItem[0] if magnitudeItem[0] < @earliest
@latest = magnitudeItem[0] if magnitudeItem[0] > @latest
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.