在Javascript中从数组中获取最小值?
问题描述:
数组 justPrices 具有如下值:
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?
答
Jon Resig 在 这篇文章 中说明如何通过扩展 Array 原型并调用底层 Math.min 方法来实现这一点不幸的是,它不采用数组,而是采用可变数量的参数:
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 );
};
然后:
var minimum = Array.min(array);