lisovaccaro lisovaccaro - 2 months ago 11
Javascript Question

Obtain smallest value from array in Javascript?

Array justPrices has values such as:

[0] = 1.5
[1] = 4.5
[2] = 9.9.


How do I return the smallest value in the array?

Answer

Jon Resig illustrated in this article how this could be achieved by extending the Array prototype and invoking the underlying Math.min method which unfortunately doesn't take an array but a variable number of arguments:

Array.min = function( array ){
    return Math.min.apply( Math, array );
};

and then:

var minimum = Array.min(array);