简体   繁体   中英

How to check if object property exists with a variable holding the property name?

I am checking for the existence of an object property with a variable holding the property name in question.

var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";

if(myObj.myProp){
    alert("yes, i have that property");
};

This is undefined because it's looking for myObj.myProp but I want it to check for myObj.prop

I am checking for the existence of an object property with a variable holding the property name in question.

var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";

if(myObj.myProp){
    alert("yes, i have that property");
};

This is undefined because it's looking for myObj.myProp but I want it to check for myObj.prop

I am checking for the existence of an object property with a variable holding the property name in question.

var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";

if(myObj.myProp){
    alert("yes, i have that property");
};

This is undefined because it's looking for myObj.myProp but I want it to check for myObj.prop

I am checking for the existence of an object property with a variable holding the property name in question.

var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";

if(myObj.myProp){
    alert("yes, i have that property");
};

This is undefined because it's looking for myObj.myProp but I want it to check for myObj.prop

I am checking for the existence of an object property with a variable holding the property name in question.

var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";

if(myObj.myProp){
    alert("yes, i have that property");
};

This is undefined because it's looking for myObj.myProp but I want it to check for myObj.prop

I am checking for the existence of an object property with a variable holding the property name in question.

var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";

if(myObj.myProp){
    alert("yes, i have that property");
};

This is undefined because it's looking for myObj.myProp but I want it to check for myObj.prop

I am checking for the existence of an object property with a variable holding the property name in question.

var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";

if(myObj.myProp){
    alert("yes, i have that property");
};

This is undefined because it's looking for myObj.myProp but I want it to check for myObj.prop

there are much simpler solutions and I don't see any answer to your actual question:

"it's looking for myObj.myProp but I want it to check for myObj.prop"

  1. to obtain a property value from a variable, use bracket notation .
  2. to test that property for truthy values, use optional chaining
  3. to return a boolean, use double-not / bang-bang / (!!)
  4. use the in operator if you are certain you have an object and only want to check for the existence of the property ( true even if prop value is undefined). or perhaps combine with nullish coalescing operator ?? to avoid errors being thrown.

 var myBadObj = undefined; var myGoodObj = {prop:"exists"} var myProp = "prop"; //1 use brackets. myGoodObj.myProp && console.log("wrong"); //dot is incorrect here //(myBadObj[myProp]) //this would throw because undefined myGoodObj[myProp] && console.log("1 - yes, i have that property"); // 2 use optional chaining. tolerates undefined myBadObj myBadObj?.[myProp] && console.log("2 - myBadObj has that"); myGoodObj?.[myProp] && console.log("2 - myGoodObj has that"); //3 get a boolean from the truthy value console.log(3, !!myBadObj?.[myProp]); console.log(3, !!myGoodObj?.[myProp]); //4 use in operator //console.log(4, myProp in myBadObj); // would throw console.log(4, myProp in {prop:undefined}); console.log(4, myProp in myGoodObj); console.log(4, myProp in (myBadObj ?? {})); //5 probably don't use hasOwnProperty() myProp = "hasOwnProperty"; // doesn't catch inherited properties (ex: hasOwnProperty is itself inherited) console.log(5, myGoodObj.hasOwnProperty(myProp)); // false :( // intolerant of undefined obj console.log(5, myBadObj.hasOwnProperty(myProp)); // throws because undefined :(

Using the Object.hasOwn method is also an alternative and it's intention is to replace the Object.hasOwnProperty method.

This static method returns true if the specified object has the indicated property as its own property or false if the property is inherited or does not exist on that object.

Please not that you must check the Browser compatibility table carefully before using this in production since it's still considered an experimental technology and is not fully supported yet by all browsers (soon to be though)

 var myObj = {}; myObj.myProp = "exists"; if (Object.hasOwn(myObj, 'myProp')){ alert("yes, i have that property"); }

More about Object.hasOwn - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn

Object.hasOwn browser compatibility - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn#browser_compatibility

I am checking for the existence of an object property with a variable holding the property name in question.

var myObj;
myObj.prop = "exists";
var myProp = "p"+"r"+"o"+"p";

if(myObj.myProp){
    alert("yes, i have that property");
};

This is undefined because it's looking for myObj.myProp but I want it to check for myObj.prop

In the answers I didn't see the !! operator

if (!!myObj.myProp) //Do something

Working for me.

if (typeof receviedData?.d?.heartbeat_interval != "undefined") {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM