Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17586

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

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

Upvotes: 1

Views: 4551

Answers (2)

James Allardice
James Allardice

Reputation: 166031

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:

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

Upvotes: 4

Eduardo
Eduardo

Reputation: 8402

I program in ActionScript, which is a similar dialect, so I am almost sure it will be the same. Please try:

if(arr[0] != null)

Note that there is a difference between arr[0] and arr["0"]

Upvotes: 0

Related Questions