简体   繁体   English

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

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

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

You will have to iterate over the object keys, and check the value of each one: 您将必须遍历对象键,并检查每个键的值

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

Depending on whether you care about properties that may have been inherited from the prototype , you could add a hasOwnProperty check in there: 根据您是否关心可能已经从prototype继承的属性,可以在其中添加hasOwnProperty检查:

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

I program in ActionScript, which is a similar dialect, so I am almost sure it will be the same. 我使用ActionScript编程,这是一种类似的方言,因此我几乎可以肯定它是相同的。 Please try: 请试试:

if(arr[0] != null)

Note that there is a difference between arr[0] and arr["0"] 请注意, arr[0]arr["0"]之间存在差异

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM