javascript jQuery从列表中获取最大的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3688373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery get biggest number from list
提问by James
var one = 1415;
var two = 2343;
var three = 11;
How to get the biggest number from these variables?
如何从这些变量中获得最大的数字?
回答by shabunc
Math.max(one, two, three)
Math.max(一、二、三)
回答by RightSaidFred
If you have them in an array, you can do this:
如果你把它们放在一个数组中,你可以这样做:
var numbers_array = [1415, 2343, 11];
numbers_array.push( 432 ); // now the array is [1415, 2343, 11, 432]
var biggest = Math.max.apply( null, numbers_array );
回答by Kevin
If your values are in an array, try reduce :
如果您的值在数组中,请尝试 reduce :
var biggestValue = myArray.reduce( function(a,b){ return a > b ? a : b ; } );
回答by Shivam Srivastava
That will work 100%
这将 100%
var max = Math.max.apply(Math, "your array");
回答by Todd Yandell
Put them in an array, sort them, and take the last of the sorted values:
把它们放在一个数组中,对它们进行排序,然后取最后一个排序的值:
[one, two, three].sort(function (a, b) {
return a > b ? 1 : (a < b ? -1 : 0);
}).slice(-1);
回答by kennebec
function biggestNumber(){
return Math.max.apply(this,arguments);
}
var one= 1415;
var two= 2343;
var three= 11;
biggestNumber(one, two, three)
最大数量(一,二,三)
/* returned value: (Number) 2343 */
/* 返回值:(数字)2343 */
回答by sreejith unni
var number=[12,34,54,32,11,2]
var big= Math.max.apply(null,number);

