【问题标题】:How to use Math.max and Max.min functions in an Array? [duplicate]如何在数组中使用 Math.max 和 Max.min 函数? [复制]
【发布时间】:2015-03-01 16:12:42
【问题描述】:

我在数组中尝试了这种方法来获取最小值和最大值,但它不起作用。

var array = [3, 6, 1, 5, 0, -2, 3];
var min = Math.min( array ); 
var max = Math.max( array ); 
document.write(max);

【问题讨论】:

  • 将数组、最小值和最大值声明为 int

标签: javascript arrays


【解决方案1】:

试试这个。

function minmax(){
  var max = Math.max.apply(Math, arguments);
  var min = Math.min.apply(Math, arguments);
  console.log(max);  // print 6
  console.log(min); // print -2
}
minmax (3, 6, 1, 5, 0, -2, 3);

【讨论】:

    【解决方案2】:

    使用Function.apply

    min = Math.min.apply(null, array);
    min = Math.max.apply(null, array);
    

    applycall() 非常相似,除了它的参数类型 支持。您可以使用参数数组而不是命名集 参数。使用apply,您可以使用数组文字

    【讨论】:

    • 我试试这个,但是不行
    猜你喜欢
    • 1970-01-01
    • 2020-01-04
    • 2022-01-23
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2016-11-11
    • 2017-09-10
    • 1970-01-01
    相关资源
    最近更新 更多