I've found lots of similar posts, but none yet that fully satisfy the question: How can I get the min & max values from the following 2D array in javascript?
[[1,2,3],[4,5,6],[7,8,9]]
i.e., return 1 and 9.
this question isn't quite what I'm after (as the user wants to ignore col 0), and here asks only for a 1D array.
The accepted answer here asks only for the first value of each set.
Can anyone point me to the correct (accepted) method? Many thanks!
How about flattening the array, then using Math.max.apply
and Math.min.apply
:
var arr = [[1,2,3],[4,5,6],[7,8,9]].reduce(function (p, c) {
return p.concat(c);
});
var max = Math.max.apply(null, arr); // 9
var min = Math.min.apply(null, arr); // 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句