如何检查对象属性是否存在与持有属性名称的variables?

我正在检查是否存在一个对象属性与variables持有问题的属性名称。

var myObj; myObj.prop = "exists"; var myProp = "p"+"r"+"o"+"p"; if(myObj.myProp){ alert("yes, i have that property"); }; 

这是undefined因为它正在寻找myObj.myProp但我希望它检查myObj.prop

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

要么

 var myProp = 'prop'; if(myProp in myObj){ alert("yes, i have that property"); 

要么

 if('prop' in myObj){ alert("yes, i have that property"); } 

你可以使用hasOwnProperty ,但是当你使用这个方法时,你需要引用引用

 if (myObj.hasOwnProperty('myProp')) { // do something } 

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

另一种方法是运算符中使用,但是在这里也需要引用

 if ('myProp' in myObj) { // do something } 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

感谢大家的帮助,并推动摆脱评估声明。 variables需要在括号内,而不是点符号。 这工作,是干净,正确的代码。

每个variables都是:appChoice,underI,underObstr。

 if(typeof tData.tonicdata[appChoice][underI][underObstr] !== "undefined"){ //enter code here } 

检查对象是否存在属性的更安全的方法是使用空对象或对象原型来调用hasOwnProperty()

 var foo = { hasOwnProperty: function() { return false; }, bar: 'Here be dragons' }; foo.hasOwnProperty('bar'); // always returns false // Use another Object's hasOwnProperty and call it with 'this' set to foo ({}).hasOwnProperty.call(foo, 'bar'); // true // It's also possible to use the hasOwnProperty property from the Object // prototype for this purpose Object.prototype.hasOwnProperty.call(foo, 'bar'); // true 

MDN Web Docs参考- Object.prototype.hasOwnProperty()

对于自己的财产:

 var loan = { amount: 150 }; if(Object.prototype.hasOwnProperty.call(loan, "amount")) { //will execute } 

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

要在结果中包含inheritance的属性,请使用in运算符:

 const yoshi = { skulk: true }; const hattori = { sneak: true }; const kuma = { creep: true }; if ("skulk" in yoshi) console.log("Yoshi can skulk"); if (!("sneak" in yoshi)) console.log("Yoshi cannot sneak"); if (!("creep" in yoshi)) console.log("Yoshi cannot creep"); Object.setPrototypeOf(yoshi, hattori); if ("sneak" in yoshi) console.log("Yoshi can now sneak"); if (!("creep" in hattori)) console.log("Hattori cannot creep"); Object.setPrototypeOf(hattori, kuma); if ("creep" in hattori) console.log("Hattori can now creep"); if ("creep" in yoshi) console.log("Yoshi can also creep"); 

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

注意:可能会试图使用typeof和[]属性访问器作为下面的代码不起作用

 var loan = { amount: 150 }; loan.installment = undefined; if("installment" in loan) // correct { // will execute } if(typeof loan["installment"] !== "undefined") // incorrect { // will not execute } 

你可以使用hasOwnProperty()和运算符。

你也可以使用我写的这个object-hasOwnProperty组件来避免在地方复制它。 它可以用来确定对象是否具有指定的属性。

例子:

 hasOwnProperty({foo: 'bar'}, 'foo') // => true hasOwnProperty({foo: 'bar'}, 'bar') // => false 

怎么运行的:

 function hasOwnProperty(obj: {}, prop: string|number): boolean { return Object.prototype.hasOwnProperty.call(obj, prop); }; 

关于什么? !!myObject['myProp']适用于我。

 if(myObj[myProp]) { `enter code here` }