Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if an object has a specific property in JavaScript?

Tags:

javascript

How do I check if an object has a specific property in JavaScript?

Consider:

x = {'key': 1}; if ( x.hasOwnProperty('key') ) {     //Do this } 

Is that the best way to do it?

like image 786
sheats Avatar asked Sep 25 '08 19:09

sheats


People also ask

How can you tell if an object has a specific property?

The first way is to invoke object. hasOwnProperty(propName) . The method returns true if the propName exists inside object , and false otherwise. hasOwnProperty() searches only within the own properties of the object.

How do you check if an object is a specific type JavaScript?

The JavaScript instanceof operator is used to check the type of an object at the run time. It returns a boolean value(true or false). If the returned value is true, then it indicates that the object is an instance of a particular class and if the returned value is false then it is not.

How do you check if an object has a specific key?

There are mainly two methods to check the existence of a key in JavaScript Object. The first one is using “in operator” and the second one is using “hasOwnProperty() method”. Method 1: Using 'in' operator: The in operator returns a boolean value if the specified property is in the object.


2 Answers

I'm really confused by the answers that have been given - most of them are just outright incorrect. Of course you can have object properties that have undefined, null, or false values. So simply reducing the property check to typeof this[property] or, even worse, x.key will give you completely misleading results.

It depends on what you're looking for. If you want to know if an object physically contains a property (and it is not coming from somewhere up on the prototype chain) then object.hasOwnProperty is the way to go. All modern browsers support it. (It was missing in older versions of Safari - 2.0.1 and older - but those versions of the browser are rarely used any more.)

If what you're looking for is if an object has a property on it that is iterable (when you iterate over the properties of the object, it will appear) then doing: prop in object will give you your desired effect.

Since using hasOwnProperty is probably what you want, and considering that you may want a fallback method, I present to you the following solution:

var obj = {     a: undefined,     b: null,     c: false };  // a, b, c all found for ( var prop in obj ) {     document.writeln( "Object1: " + prop ); }  function Class(){     this.a = undefined;     this.b = null;     this.c = false; }  Class.prototype = {     a: undefined,     b: true,     c: true,     d: true,     e: true };  var obj2 = new Class();  // a, b, c, d, e found for ( var prop in obj2 ) {     document.writeln( "Object2: " + prop ); }  function hasOwnProperty(obj, prop) {     var proto = obj.__proto__ || obj.constructor.prototype;     return (prop in obj) &&         (!(prop in proto) || proto[prop] !== obj[prop]); }  if ( Object.prototype.hasOwnProperty ) {     var hasOwnProperty = function(obj, prop) {         return obj.hasOwnProperty(prop);     } }  // a, b, c found in modern browsers // b, c found in Safari 2.0.1 and older for ( var prop in obj2 ) {     if ( hasOwnProperty(obj2, prop) ) {         document.writeln( "Object2 w/ hasOwn: " + prop );     } } 

The above is a working, cross-browser, solution to hasOwnProperty, with one caveat: It is unable to distinguish between cases where an identical property is on the prototype and on the instance - it just assumes that it's coming from the prototype. You could shift it to be more lenient or strict, based upon your situation, but at the very least this should be more helpful.

like image 199
John Resig Avatar answered Sep 21 '22 23:09

John Resig


With Underscore.js or (even better) Lodash:

_.has(x, 'key'); 

Which calls Object.prototype.hasOwnProperty, but (a) is shorter to type, and (b) uses "a safe reference to hasOwnProperty" (i.e. it works even if hasOwnProperty is overwritten).

In particular, Lodash defines _.has as:

function has(object, key) {   return object ? hasOwnProperty.call(object, key) : false; } // hasOwnProperty = Object.prototype.hasOwnProperty 
like image 36
Brian M. Hunt Avatar answered Sep 22 '22 23:09

Brian M. Hunt