我正在尝试获取数字数组中的最大值:
maxtime=Math.max.apply( Math, cnttimearr );
alert(maxtime);
但是,我得到的是 NaN
而不是最大值。谁能告诉我我做错了什么?
最佳答案
阅读the manual .
If at least one of arguments cannot be converted to a number, the result is
NaN
.
确保数组中的所有元素都可以转换为数字。
> xs = [1, 2, '3'];
[1, 2, "3"]
> Math.max.apply(Math, xs);
3
> xs = [1, 2, 'hello'];
[1, 2, "hello"]
> Math.max.apply(Math, xs);
NaN
关于javascript - 在 JavaScript 中使用 Math.max() 函数时如何避免 NaN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20868508/