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.

akirk
  • 6,757
  • 2
  • 34
  • 57
Starkers
  • 10,273
  • 21
  • 95
  • 158

3 Answers3

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.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 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. – cookie monster Mar 20 '14 at 23:14
  • 1
    @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()`. – jfriend00 Mar 20 '14 at 23:15
  • 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. – cookie monster Mar 20 '14 at 23:19
  • @cookiemonster - I've removed any mention of enumerability. – jfriend00 Mar 20 '14 at 23:21
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.

Steven de Salas
  • 20,944
  • 9
  • 74
  • 82
  • 1
    "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. – jfriend00 Mar 20 '14 at 23:17
  • This is a perfectly legit answer. Doesn't deserve downvotes, IMO. – cookie monster Mar 20 '14 at 23:27
0

You could use typeof:

function inObject(obj, key) {
    return typeof obj[key] != "undefined";
}
cookie monster
  • 10,671
  • 4
  • 31
  • 45
akirk
  • 6,757
  • 2
  • 34
  • 57
  • 2
    you don't need typeof since accessing an object property that doesn't exists will give undefined . So you can use obj[key] !== undefined – progysm Mar 20 '14 at 23:20