在Javascript中从数组中获取最小值? [英] Obtain smallest value from array in Javascript?

查看:26
本文介绍了在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);

这篇关于在Javascript中从数组中获取最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆