检查数组中是否包含除null之外的其他内容? [英] Check if an array contains something other than null in javascript?

查看:67
本文介绍了检查数组中是否包含除null之外的其他内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很可能总是这样的数组:

I have an array that will most likely always look like:

[null, null, null, null, null]

有时这个数组可能会改为:

sometimes this array might change to something like:

["helloworld", null, null, null, null]

我知道我可以使用for循环,但有没有办法使用 indexOf 来检查数组中的某些内容是否等于null。

I know I could use a for loop for this but is there a way to use indexOf to check if something in an array that is not equal to null.

我正在寻找类似的东西:

I am looking for something like:

var index = indexof(!null);


推荐答案

使用 some 返回一个boolean:

Use some which returns a boolean:

var arr = [null, 2, null, null];

var otherThanNull = arr.some(function (el) {
    return el !== null;
}); // true

DEMO

这篇关于检查数组中是否包含除null之外的其他内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆