简体   繁体   English

如何遍历数组对象并检查每个元素以查看是否已定义?

[英]How can I loop through an array object and check each element to see if one is defined?

I have an array object that looks like this: 我有一个看起来像这样的数组对象:

var myArr = [undefined, undefined, undefined, undefined, undefined];

How can I loop through the array object and call a function if any one of the elements in the array is defined and not null? 如果定义了数组中的任何元素且不为null,如何遍历数组对象并调用函数?

If you don't care what the valid value is: 如果您不在乎有效值是什么:

var myFunction = function(){console.log("array has valid values");};
var myArr = [undefined, undefined, undefined, undefined, 2];
var hasUndefined = myArr.some(function(item){
    return typeof item !== "undefined";
});
if (hasUndefined){
    myFunction();
}

If you need to find out what the valid items are: 如果您需要找出有效项目是什么:

var myFunction = function(validItems){
    console.log("array has valid values, and here they are:",validItems);
};
var myArr = [undefined, undefined, undefined, undefined, 2];
var validItems = myArr.filter(function(item){return typeof item !== "undefined";})
if (validItems.length){
    myFunction(validItems);
}
var myArr = [null,undefined,4,5];

for (var i in myArr) {
    if (myArr[i]) {
        //Do something..
        return;
    }
}

I've also seen this which seems a very nice way. 我也看到了这似乎是一个非常好的方法。

[null,undefined,4,5].every(function(e) {
    if (e) {
        //Do something..
        return false;
    }
    else return true;
});

Is there a standard function to check for null, undefined, or blank variables in JavaScript? 是否有标准函数检查JavaScript中的null,undefined或空白变量?

Try this 尝试这个

function isNotUndefined(item) {
    return typeof item != "undefined";
}

if (myArr.some(isNotUndefined)) {
   //do something
}

In this code I use the Array.prototype.some function to check if any element is defined (to do this I use an extra function called isNotUndefined ). 在此代码中,我使用Array.prototype.some函数检查是否定义了任何元素(为此,我使用了一个名为isNotUndefined的附加函数)。

Try this. 尝试这个。

var myArr = [undefined, null, 123, undefined, null, 456];//for test
myArrFilterd = myArr.filter(function(x){return x!=null?true:false;});
myArrFilterd.forEach(function(x){console.log(x);/*something you want to do for each element*/});

output: 输出:

123
456

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何循环遍历 object 并获取每个元素的索引并将其存储在数组中? - How can I loop through an object and get the index of each element and store it in an array? 如何循环遍历 object 数组并从数组中列出的两个对象中获取一个属性元素? - How can I loop through an array of object and get one property element out of the two objects listed inside the array? 如何遍历一个jQuery数组,然后一次输出一项(从索引0开始)? - How can I loop through a jquery array and then output each item (starting at index 0) one at a time? 如何向对象添加数组,但是为每个数组元素过滤除了一列以外的所有数组? - How can I add an array to an object but filter out all but one column for each array element? 我如何循环遍历数组中每个 object 的属性,每个 object 的属性都不同。 这是 Javascript - How do i loop through the properties of each object in the array, the properties are different in each object. This is Javascript 遍历对象数组并将每个元素插入html - Loop through object array and insert each element into html 如何遍历每个元素上调用 jquery.each 的数组? - How to loop through an array calling jquery.each on each element? 如何在对象数组上使用 jQuery.each() 循环 - How can I use jQuery.each() loop on Array of Object 如何检查数组的至少一项是否包含在 object 的数组中并遍历所有对象(JS) - How to check if at least one item of an array is included in an array of an object and loop through all objects (JS) 如何循环遍历 object 中的嵌套数组? - How can I loop through a nested array within an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM