2

Something like this:

object = { 'one' : 1, 'two' : 2 }
$.inObject(object, 'one') // returns true
$.inObject(object, 'three') // returns false

It would only search the keys.

3
  • 1
    Just use JavaScript: object.hasOwnProperty('one') Commented Mar 20, 2014 at 23:08
  • Here is a post that answers the question - stackoverflow.com/questions/1758937/… Commented Mar 20, 2014 at 23:08
  • $.inArray looks for values, whereas you seem to want to look for properties. Not sure which you actually want. Commented Mar 20, 2014 at 23:12

3 Answers 3

8

The safest way to do this is using .hasOwnProperty():

obj = { 'one' : 1, 'two' : 2 };
obj.hasOwnProperty('one') // returns true
obj.hasOwnProperty('three') // returns false

You can also use the in operator as others have suggested like this:

obj = { 'one' : 1, 'two' : 2 };
console.log('one' in obj)   // true
console.log('three' in obj) // false

But, the in operator can be confused by any properties on the prototype of the object that might have been added to Object by some library or other code so hasOwnProperty() is generally considered the safer option for detecting things you added to the actual object yourself. In otherwords, if anyone added an method or property to the Object prototype, it would get picked up by the in operator, but not by .hasOwnProperty() which only checks the actual object itself, not any inherited properties.


FYI, this may be overkill for what you need for your particular use, but there's an implementation of a Set object and a ValueSet object that uses this type of logic and offers lots of typical Set operations here: Mimicking sets in JavaScript?. The code is all available for the Set object too so you can see how it all works if you want to educate yourself further.

Sign up to request clarification or add additional context in comments.

4 Comments

The clash with in isn't only with enumerable methods. Any property on the object or inherited will return true, irrespective of enumerability. However, this isn't necessarily less safe. Could be that in some cases inherited properties should be included.
@cookiemonster - in the OP's example, they clearly only want properties that they've defined themselves on the object to be included here and that is fairly typical which is why it is generally better to use .hasOwnProperty().
Just saying that it depends on the requirements. Still, whether a property is enumerable will have no impact on the result of the in operator. It isn't confused. It's just working as intended.
@cookiemonster - I've removed any mention of enumerability.
5

Yep, the in keyword. No need to use jQuery:

object = { 'one' : 1, 'two' : 2 };
alert('one' in object); // true
alert('three' in object); // false

You can also use object.hasOwnProperty('blah') which is more reliable. Douglas Crockford prefers this approach, you will find JSLint complaining about this occassionally. I prefer the in notation as its easier to read.

2 Comments

"Strange results" with .hasOwnProperty()? You mean .hasOwnProperty() will only return true if you actually added the property directly to the object yourself and not if some library you're using added it to the Object's prototype - which is what you usually want and what the OP appears to want.
This is a perfectly legit answer. Doesn't deserve downvotes, IMO.
0

You could use typeof:

function inObject(obj, key) {
    return typeof obj[key] != "undefined";
}

1 Comment

you don't need typeof since accessing an object property that doesn't exists will give undefined . So you can use obj[key] !== undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.