RienNeVaPlu͢s
RienNeVaPlu͢s

Reputation: 7632

How to efficiently detect the existence of any keys of an object in JavaScript?

What's the fastest way of detecting the existence of one or more keys inside an object? Is it possible to do it without iterating over the object by using Object.keys().length?
This question is almost identical to How to efficiently count the number of keys/properties of an object in JavaScript?

Upvotes: 2

Views: 87

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149030

Perhaps use this:

function hasProperties (obj) {
    for(var x in obj) 
        return true; 
    return false;
}

Yes, admittedly you do have to iterate over the object, but you stop after the first property is found.

Upvotes: 3

Related Questions