1
2
3 |
var
a=[1,2,3,5]; alert(Math.max.apply( null , a)); //最大值 alert(Math.min.apply( null , a)); //最小值 |
多维数组如下
1
2
3
4 |
var
a = [1, 2, 3, [5, 6], [1, 4, 8]]; var
ta = a. join ( "," ).split( "," ); //转化为一维数组 alert(Math.max.apply( null , ta)); //最大值 alert(Math.min.apply( null , ta)); //最小值 |
或者用下面这种添加属性的方法
1
2
3
4
5
6
7
8
9 |
Array.prototype.max = function () { return
Math.max.apply({}, this ) } Array.prototype.min = function () { return
Math.min.apply({}, this ) } $(function () { alert([1,2,3,4,5,11].max()); }); |
js中快速获取数组中的最大值最小值,布布扣,bubuko.com
原文:http://www.cnblogs.com/TivonStone/p/3597908.html