检查数组中是否包含除null之外的其他内容?

时间:2022-08-24 16:28:47

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]

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.

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

I am looking for something like:

我正在寻找类似的东西:

var index = indexof(!null);

4 个解决方案

#1


18  

Use some which returns a boolean:

使用一些返回布尔值:

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

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

DEMO

#2


3  

In recent versions of Chrome, Safari, and Firefox (and future versions of other browsers), you can use findIndex() to find the index of the first non-null element.

在最新版本的Chrome,Safari和Firefox(以及其他浏览器的未来版本)中,您可以使用findIndex()来查找第一个非null元素的索引。

var arr = [null, null, "not null", null];

var first = arr.findIndex( 
              function(el) { 
                return (el !== null);
              }
            );

console.log(first);

(for other browsers, there's a polyfill for findIndex())

(对于其他浏览器,findIndex()有一个polyfill)

#3


2  

You can use Array.prototype.some to check if there are any elements matching a function:

您可以使用Array.prototype.some来检查是否存在与函数匹配的元素:

var array = [null, null, 2, null];
var hasValue = array.some(function(value) {
    return value !== null;
});
document.write('Has Value? ' + hasValue);

If you want the first index of a non-null element, you'll have to get a bit trickier. First, map each element to true / false, then get the indexOf true:

如果你想要一个非null元素的第一个索引,你将不得不变得有点棘手。首先,将每个元素映射为true / false,然后获取indexOf为true:

var array = [null, null, 2, null, 3];
var index = array
    .map(function(value) { return value !== null })
    .indexOf(true);
document.write('Non-Null Index Is: ' + index);

#4


0  

This does the work

这样做了

var array = ['hello',null,null];
var array2 = [null,null,null];

$.each(array, function(i, v){ 
    if(!(v == null)) alert('Contains data');
})

#1


18  

Use some which returns a boolean:

使用一些返回布尔值:

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

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

DEMO

#2


3  

In recent versions of Chrome, Safari, and Firefox (and future versions of other browsers), you can use findIndex() to find the index of the first non-null element.

在最新版本的Chrome,Safari和Firefox(以及其他浏览器的未来版本)中,您可以使用findIndex()来查找第一个非null元素的索引。

var arr = [null, null, "not null", null];

var first = arr.findIndex( 
              function(el) { 
                return (el !== null);
              }
            );

console.log(first);

(for other browsers, there's a polyfill for findIndex())

(对于其他浏览器,findIndex()有一个polyfill)

#3


2  

You can use Array.prototype.some to check if there are any elements matching a function:

您可以使用Array.prototype.some来检查是否存在与函数匹配的元素:

var array = [null, null, 2, null];
var hasValue = array.some(function(value) {
    return value !== null;
});
document.write('Has Value? ' + hasValue);

If you want the first index of a non-null element, you'll have to get a bit trickier. First, map each element to true / false, then get the indexOf true:

如果你想要一个非null元素的第一个索引,你将不得不变得有点棘手。首先,将每个元素映射为true / false,然后获取indexOf为true:

var array = [null, null, 2, null, 3];
var index = array
    .map(function(value) { return value !== null })
    .indexOf(true);
document.write('Non-Null Index Is: ' + index);

#4


0  

This does the work

这样做了

var array = ['hello',null,null];
var array2 = [null,null,null];

$.each(array, function(i, v){ 
    if(!(v == null)) alert('Contains data');
})