Karan Hudia
Karan Hudia

Reputation: 573

What is the best and fastest way in JS to validate if an object exists and if it does then a certain property exists on it as well?

I have to put a validity check before performing any operation using the object which -

  1. If that object exists.
  2. If its exists then a certain property also exists on it.

For ex-

var obj = {
    key: "value"
}

Now the most conventional way to do this is-

if(obj) {
    if(obj.hasOwnProperty('key') {
        //Do some action (validity check pass)
        //For Example- console.log(obj.key);
    }
}

But i am looking for a more faster and efficient way there must be to solve this.

Upvotes: 1

Views: 1078

Answers (4)

Odalrick
Odalrick

Reputation: 645

If you care about the protoype chain, then no there isn't a better way; you have to check every link in the chain down.

If not, and the properties usually exist it may be worth just accessing the property and catching any exception, but use performance testing to see.

But I'd say the issue is that you aren't restrictive enough with your input: require the caller of your code to provide an object and let them handle it.

To me it "smells" like two separate issues: "Does the object exist?", if so provide a default and "Does the object have this property?", the business logic.

If so, use a standard pattern to set the default: "obj = obj || {};" for instance; and then the test becomes a single call, hard to make "faster".

Upvotes: 0

Laszlo
Laszlo

Reputation: 2328

You can definitely combine the two statements into one as @Stuart and @phuzi mentioned:

if(!!obj && obj.hasOwnProperty('key')

also hasOwnProperty will ignore all inherited properties that come through the prototype. If you'd prefer to include the inherited properties the in operator can be used here.

if (!!ob && (prop in ob))

Upvotes: 1

subkonstrukt
subkonstrukt

Reputation: 446

Like this ?

var obj = {
    key: "value"
}
var objx = null;


if(obj && "key" in obj){
	document.getElementById("check_one").innerHTML = "is valid";
}else{
	document.getElementById("check_one").innerHTML ="is not valid";
}
if(obj && "notkey" in obj){
	document.getElementById("check_two").innerHTML = "is valid";
}else{
	document.getElementById("check_two").innerHTML ="is not valid";
}

if(objx && "key" in objx){
	document.getElementById("check_three").innerHTML = "is valid";
}else{
	document.getElementById("check_three").innerHTML ="is not valid";
}
<p>
Check One (should be valid): <span id="check_one"></span>
</p>
<p>
Check Two (should be invalid): <span id="check_two"></span>
</p>
<p>
Check Three (should be invalid) <span id="check_three"></span>
</p>

Depending on your required browser support you could also use Reflect.has

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/has

like

Reflect.has(obj, 'key');

if you want to support older IEs I guess you will have to go with HasOwnProperty, there wont be any other possibility - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

Upvotes: 1

Stuart
Stuart

Reputation: 6780

I'd do:

if ( typeof obj != "undefined" && obj.hasOwnProperty('key') ) 
{ 
    console.log('found'); 
} 
else 
{ 
    console.log('not found');
}

Upvotes: 1

Related Questions