如何检查对象属性的任何值是否等于0

How can I check if any of the values of the object properties are equal to 0?

本文关键字:任何值 是否 属性 对象 何检查 检查      更新时间:2023-09-26

如何检查对象属性的任何值是否等于0?

您必须在对象键上迭代,并检查每个键的值

for(var p in x) {
    if(x[p] === 0) {
        console.log("Found!");
    }
}

根据您是否关心可能从prototype继承的属性,您可以在其中添加hasOwnProperty检查:

for(var p in x) {
    if(x.hasOwnProperty(p)) {
        if(x[p] === 0) {
            //Found it!
        }
    }
}

我用ActionScript编程,这是一种类似的方言,所以我几乎可以肯定它会是一样的。请尝试:

if(arr[0] != null)

请注意,arr[0]arr["0"] 之间存在差异