索引28:如何删除此“ NaN”值。不能使用,isNaN因为我想要字符串和数字。但不是NaN尝试过:typeof value === 'undefined'
value == null没有成功
3 回答
跃然一笑
TA贡献1826条经验 获得超6个赞
您可以NaN通过使用进行特定的测试Number.isNaN,这与plain略有不同isNaN:仅当其参数为数字(其值为NaN)时,它才返回true。换句话说,它将不会尝试将字符串和其他值强制转换为数字。
演示:
const values = [
12,
NaN,
"hello",
{ foo: "bar" },
NaN,
null,
undefined,
-3.14,
];
const filtered = values.filter(x => !Number.isNaN(x));
console.log(filtered);
Number.isNaN是ECMAScript 6中的新增功能。除Internet Explorer之外,所有浏览器均支持该功能。如果您需要支持IE,这是一个简单的解决方法:
if (!Number.isNaN) {
Number.isNaN = function (x) { return x !== x; };
}
千巷猫影
TA贡献1829条经验 获得超7个赞
您可以结合使用typeof(检查数字)isNaN
注意typeof NaN返回"number"
typeof x === "number" && isNaN(x)
另一个解决方案是使用Number.isNaN,它将不会尝试将参数转换为数字。因此,true仅当参数为NaN
肥皂起泡泡
TA贡献1829条经验 获得超6个赞
添加回答
举报
0/150
提交
取消
