检查数组中是否包含除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