javascript 未定义条目上的 Math.max 和 Math.min NaN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12957405/
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
Math.max and Math.min NaN on undefined entry
提问by Hymantheripper
When passing values to the Math.max or Math.min function in JavaScript, they return the highest and lowest values from the input respectively.
在 JavaScript 中将值传递给 Math.max 或 Math.min 函数时,它们分别返回输入中的最高值和最低值。
However, if a piece of data that is undefined is entered, e.g.
但是,如果输入了一段未定义的数据,例如
Math.max(5,10,undefined);
The result returned is NaN. Is there a simply way to fix this using JS/jQuery?
返回的结果是 NaN。有没有一种简单的方法可以使用 JS/jQuery 解决这个问题?
回答by I Hate Lazy
I assume the undefined
is actually some variable.
我认为undefined
实际上是一些变量。
You can substitute -Infinity
for any NaN
value to ensure a number.
您可以替换-Infinity
任何NaN
值以确保数字。
var foo;
Math.max(5, 10, isNaN(foo) ? -Infinity : foo); // returns 10
Same concept can be used on Math.min
, but with Infinity
:
相同的概念可用于Math.min
,但用于Infinity
:
var foo;
Math.min(5, 10, isNaN(foo) ? Infinity : foo); // returns 5
回答by VisioN
Write your own max
function:
编写自己的max
函数:
function max() {
var par = []
for (var i = 0; i < arguments.length; i++) {
if (!isNaN(arguments[i])) {
par.push(arguments[i]);
}
}
return Math.max.apply(Math, par);
}
Or shorter using array filtering:
或更短的使用数组过滤:
function max() {
var args = Array.prototype.slice.call(arguments);
return Math.max.apply(Math, args.filter(function(val) {
return !isNaN(val);
}));
}
Usage:
用法:
max(5, 10, undefined); // output: 10
回答by jbabey
Check that each number is defined before passing it to max
. Use an sentinel value as a default (such as -Infinity
). For min you would want to use Infinity
:
在将其传递给 之前检查每个数字是否已定义max
。使用标记值作为默认值(例如-Infinity
)。对于 min,您会想要使用Infinity
:
var a = 5;
var b = 10;
var c = undefined;
var defaultForMax = function (num) {
return typeof num === 'number' ? num : -Infinity;
};
c = defaultForMax(c); // c is now -Infinity
Math.max(a, b, c); // 10
回答by PhilippeP
Even simpler in my opinion, if you don't mind using d3, you could do the following:
在我看来,更简单的是,如果您不介意使用 d3,则可以执行以下操作:
d3.max([5,10,undefined]) // 10
From the d3 documentation (https://github.com/d3/d3-array#statistics):
从 d3 文档(https://github.com/d3/d3-array#statistics):
Returns the minimum value in the given iterable using natural order.
使用自然顺序返回给定迭代中的最小值。
...
...
Unlike the built-in Math.min, this method ignores undefined, null and NaN values; this is useful for ignoring missing data.In addition, elements are compared using natural order rather than numeric order. For example, the minimum of the strings [“20”, “3”] is “20”, while the minimum of the numbers [20, 3] is 3.
与内置的 Math.min 不同,此方法忽略 undefined、null 和 NaN 值;这对于忽略丢失的数据很有用。此外,元素使用自然顺序而不是数字顺序进行比较。例如,字符串 [“20”, “3”] 的最小值为“20”,而数字 [20, 3] 的最小值为 3。
回答by Wahyudiansyah Arif
using falsy inside Math.max
在 Math.max 中使用 falsy
var foo;
Math.max(5, 10, !isNaN(foo) && foo);
var foo;
Math.min(5, 10, !isNaN(foo) && foo);