试图检查数组是否有空元素或没有数字

问题描述 投票:0回答:2

尝试在其上运行Number()时检查空字符串或不是数字的元素

const latLong = ['1', 'g']

function findError(obj) {
    // console.log(Number(obj))

     return obj === '' || Number(obj) === NaN
    }
console.log(latLong.find(findError))

它似乎捕获了空字符串,但没有捕获Number()部分,将不胜感激]]

[尝试在其上运行Number()时检查空字符串或非数字元素const latLong = ['1','g'] function findError(obj){// console.log(Number (obj))return ...

javascript arrays
2个回答
0
投票

要检查Nan,您可以使用内置的isNaN()功能。检查文档here


0
投票
let latLong = ['1', 'g'];
latLong = latLong.filter(el => {
  return el === '' || isNaN(el);
});
© www.soinside.com 2019 - 2024. All rights reserved.