我有一个数组,很可能总是这样:
[null, null, null, null, null]
有时这个数组可能会变成这样:
["helloworld", null, null, null, null]
我知道我可以使用 for 循环,但是有没有办法使用indexOf
检查数组中是否有不等于 null 的内容。
我正在寻找类似的东西:
var index = indexof(!null);
Use some https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some它返回一个布尔值:
const arr = [null, 2, null, null];
const arr2 = [null, null, null, null];
function otherThanNull(arr) {
return arr.some(el => el !== null);
}
console.log(otherThanNull(arr));
console.log(otherThanNull(arr2));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)