如何使用Math.max和Max.min功能于一身的阵列? [英] How to use Math.max and Max.min functions in an Array?

查看:245
本文介绍了如何使用Math.max和Max.min功能于一身的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数组中尝试过这种方法得到的最大值和最小值,但它不能正常工作。

 变种数组= [3,6,1,5,0,-2,3];
变种分钟= Math.min(数组);
VAR最大= Math.max(数组);
的document.write(最大);


解决方案

使用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply\"相对=nofollow> Function.apply

  MIN = Math.min.apply(NULL,数组);
分= Math.max.apply(NULL,数组);


  

适用是非常相似的<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call\"相对=nofollow> ()调用 ,除了参数的类型它
  支持。您可以使用,而不是一组命名的参数数组
  参数。随着应用,您可以使用文字数组


I tried this method in an array to get min and max values but it doesn't work.

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

解决方案

Use Function.apply

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

apply is very similar to call(), except for the type of arguments it supports. You can use an arguments array instead of a named set of parameters. With apply, you can use an array literal

这篇关于如何使用Math.max和Max.min功能于一身的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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