lisovaccaro lisovaccaro - 1 year ago 67
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 Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download