Javascript 为什么 math.max() 在整数数组上返回 NaN?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32647149/
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
Why is math.max() returning NaN on an array of integers?
提问by Matt Welander
I am trying to get the highest number from a simple array:
我试图从一个简单的数组中获得最高的数字:
data = [4, 2, 6, 1, 3, 7, 5, 3];
alert(Math.max(data));
I have read that if even one of the values in the array can't be converted to number, it will return NaN, but in my case, I have double-checked with typeofto make sure they are all numbers, so what can be my problem?
我已经读过,即使数组中的值之一无法转换为数字,它也会返回NaN,但在我的情况下,我已经仔细检查typeof以确保它们都是数字,所以我的问题是什么?
回答by musefan
The reason why your code doesn't work is because Math.maxis expecting each parameter to be a valid number. This is indicated in the documentationas follows:
您的代码不起作用的原因Math.max是希望每个参数都是有效数字。这在文档中指出如下:
If at least one of arguments cannot be converted to a number, the result is NaN.
如果至少有一个参数无法转换为数字,则结果为 NaN。
In your instance you are only providing 1 argument, and that 1 value is an array not a number (it doesn't go as far as checking what is in an array, it just stops at knowing it isn't a valid number).
在您的实例中,您只提供 1 个参数,并且 1 个值是一个数组而不是数字(它不会检查数组中的内容,它只是在知道它不是有效数字时停止)。
One possible solution is to explicitly call the function by passing an array of arguments. Like so:
一种可能的解决方案是通过传递参数数组来显式调用该函数。像这样:
Math.max.apply(Math, data);
What this effectively does is the same as if you manually specified each argument without an array:
这与您手动指定每个参数而不使用数组的效果相同:
Math.max(4, 2, 6, 1, 3, 7, 5, 3);
And as you can see, each argument is now a valid number, so it will work as expected.
正如你所看到的,每个参数现在都是一个有效的数字,所以它会按预期工作。
回答by Grundy
if you see doc for Math.maxyou can see next description
如果你看到Math.max 的文档,你可以看到下一个描述
Because max() is a static method of Math, you always use it as Math.max(), rather than as a method of a Math object you created (Math is not a constructor).
If no arguments are given, the result is -Infinity.
If at least one of arguments cannot be converted to a number, the result is NaN.
因为 max() 是 Math 的静态方法,所以您总是将它用作 Math.max(),而不是用作您创建的 Math 对象的方法(Math 不是构造函数)。
如果未给出任何参数,则结果为 -Infinity。
如果至少有一个参数无法转换为数字,则结果为 NaN。
When you call Math.maxwith array parameter like
当您Math.max使用数组参数调用时
Math.max([1,2,3])
you call this function with oneparameter - [1,2,3]and javascript try convert it to number and get ("1,2,3" -> NaN) fail.
So result as expected - NaN
你用一个参数调用这个函数-[1,2,3]并且 javascript 尝试将其转换为数字并获取 ("1,2,3" -> NaN) 失败。
所以结果如预期 - NaN
NOTE:if array with just onenumber - all work correctly
注意:如果数组只有一个数字 - 一切正常
Math.max([23]) // return 23
because [23] -> "23" -> 23and covert to Number is done.
因为[23] -> "23" -> 23并隐蔽到 Number 已完成。
If you want get max element from array you should use applyfunction, like
如果你想从数组中获取最大元素,你应该使用apply函数,比如
Math.max.apply(Math,[1,2,3])
or you can use the new spread operator
或者您可以使用新的点差运算符
Math.max(...[1,2,3])
回答by Bijoy Thangaraj
It's not working because you are passing an array as the parameter instead of comma separated numbers. Try spreading the array like this:
它不起作用,因为您将数组作为参数而不是逗号分隔的数字传递。尝试像这样展开数组:
data = [4, 2, 6, 1, 3, 7, 5, 3];
alert(Math.max(...data));
回答by dpolicastro
If you have to use the Math.max function and one number of the array might be undefined, you could use:
如果您必须使用 Math.max 函数并且数组的一个数字可能未定义,则可以使用:
x = Math.max(undefined || 0, 5)
console.log(x) // prints 5

