How can I easily obtain the min or max element of a JavaScript Array?
Example Psuedocode:
let array = [100, 0, 50]
array.min() //=> 0
array.max() //=> 100
How can I easily obtain the min or max element of a JavaScript Array?
Example Psuedocode:
let array = [100, 0, 50]
array.min() //=> 0
array.max() //=> 100
If you use the library sugar.js, you can write arr.min() and arr.max() as you suggest. You can also get min and max values from non-numeric arrays.
Examples:
Libraries like Lo-Dash and underscore.js also provide similar powerful min and max functions:
Example from Lo-Dash:
How about augmenting the built-in Array object to use
Math.max
/Math.min
instead:Here is a JSFiddle.
Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just
apply
'ingMath.xxx()
to your array directly:Alternately, assuming your browser supports ECMAScript 6, you can use the spread operator which functions similarly to the
apply
method:.apply
is often used when the intention is to invoke a variadic function with a list of argument values, e.g.The
Math.max([value1[,value2, ...]])
function returns the largest of zero or more numbers.The
Math.max()
method doesn't allow you to pass in an array. If you have a list of values of which you need to get the largest, you would normally call this function using Function.prototype.apply(), e.g.However, as of the ECMAScript 6 you can use the spread operator:
Using the spread operator, the above can be rewritten as such:
When calling a function using the variadic operator, you can even add additional values, e.g.
Bonus:
Spread operator enables you to use the array literal syntax to create new arrays in situations where in ES5 you would need to fall back to imperative code, using a combination of
push
,splice
, etc.I had the same problem, I needed to obtain the minimum and maximum values of an array and, to my surprise, there were no built-in functions for arrays. After reading a lot, I decided to test the "top 3" solutions myself:
The test code was this:
The array A was filled with 100,000 random integer numbers, each function was executed 10,000 times on Mozilla Firefox 28.0 on an intel Pentium 4 2.99GHz desktop with Windows Vista. The times are in seconds, retrieved by performance.now() function. The results were these, with 3 fractional digits and standard deviation:
The REDUCE solution was 117% slower than the discrete solution. The APPLY solution was the worse, 2,118% slower than the discrete solution. Besides, as Peter observed, it doesn't work for large arrays (about more than 1,000,000 elements).
Also, to complete the tests, I tested this extended discrete code:
The timing: mean=0.218s, sd=0.094
So, it is 35% slower than the simple discrete solution, but it retrieves both the maximum and the minimum values at once (any other solution would take at least twice that to retrieve them). Once the OP needed both values, the discrete solution would be the best choice (even as two separate functions, one for calculating maximum and another for calculating minimum, they would outperform the second best, the REDUCE solution).
The following code works for me :
If you are using prototype.js framework, then this code will work ok:
Documented here: Javascript prototype framework for max