This question already has an answer here:
这个问题已经有了答案:
- Dynamically access object property using variable 10 answers
- 使用变量10回答动态访问对象属性
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
这个没有定义,因为它在寻找myObj。我的道具,但我想让它检查我的道具
9 个解决方案
#1
874
var myProp = 'prop';
if(myObj.hasOwnProperty(myProp)){
alert("yes, i have that property");
}
Or
或
var myProp = 'prop';
if(myProp in myObj){
alert("yes, i have that property");
Or
或
if('prop' in myObj){
alert("yes, i have that property");
}
#2
30
You can use hasOwnProperty, but based on the reference you need quotes when using this method:
您可以使用hasOwnProperty,但是在使用此方法时需要引用引用:
if (myObj.hasOwnProperty('myProp')) {
// do something
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
Another way is to use in operator, but you need quotes here as well:
另一种方法是在操作符中使用,但这里也需要引号:
if ('myProp' in myObj) {
// do something
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
#3
19
Thank you for everyone's assistance and pushing to get rid of the eval statement. Variables needed to be in brackets, not dot notation. This works and is clean, proper code.
感谢大家对eval语句的帮助和推动。变量需要放在括号中,而不是点符号。这是可行的,并且是干净的,正确的代码。
Each of these are variables: appChoice, underI, underObstr.
每一个都是变量:appChoice, underI, underObstr。
if(typeof tData.tonicdata[appChoice][underI][underObstr] !== "undefined"){
//enter code here
}
#4
9
A much more secure way to check if property exists on the object is to use empty object or object prototype to call hasOwnProperty()
检查对象上是否存在属性的更安全的方法是使用空对象或对象原型来调用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
Reference from MDN Web Docs - Object.prototype.hasOwnProperty()
来自MDN网络文档的引用- Object.prototype.hasOwnProperty()
#5
3
For own property :
为自己的财产:
var loan = { amount: 150 };
if(Object.prototype.hasOwnProperty.call(loan, "amount"))
{
//will execute
}
Note: using Object.prototype.hasOwnProperty is better than loan.hasOwnProperty(..), in case a custom hasOwnProperty is defined in the prototype chain (which is not the case here), like
注意:使用Object.prototype。hasOwnProperty比loan.hasOwnProperty(..)要好,如果在prototype链中定义了定制的hasOwnProperty(这里不是这样),比如
var foo = {
hasOwnProperty: function() {
return false;
},
bar: 'Here be dragons'
};
/ / https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
To include inherited properties in the finding use the in operator: (but you must place an object at the right side of 'in', primitive values will throw error, e.g. 'length' in 'home' will throw error, but 'length' in new String('home') won't)
要在查找中包含继承的属性,请使用in运算符:(但是必须将对象放置在“in”的右边,原始值将抛出错误,例如:“home”中的“length”会出错,而“length”在新字符串(“home”)中不会出错。
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
/ / https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
Note: One may be tempted to use typeof and [ ] property accessor as the following code which doesn't work ...
注意:您可能想要使用typeof和[]属性访问器,因为下面的代码不起作用……
var loan = { amount: 150 };
loan.installment = undefined;
if("installment" in loan) // correct
{
// will execute
}
if(typeof loan["installment"] !== "undefined") // incorrect
{
// will not execute
}
#6
-1
You can use hasOwnProperty()
as well as in
operator.
可以使用hasOwnProperty()和in运算符。
#7
-2
You can also use this object-hasOwnProperty component I wrote to avoid copy-pasting it across places. It can be used to determine whether the object has the specified property.
您还可以使用我编写的object-hasOwnProperty组件,以避免在各处复制粘贴它。它可以用来确定对象是否具有指定的属性。
Examples:
例子:
hasOwnProperty({foo: 'bar'}, 'foo') // => true
hasOwnProperty({foo: 'bar'}, 'bar') // => false
How it works:
它是如何工作的:
function hasOwnProperty(obj: {}, prop: string|number): boolean {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
#8
-9
what about? !!myObject['myProp']
works for me.
关于什么?! !myObject myProp的适合我。
#9
-11
if(myObj[myProp])
{
`enter code here`
}
#1
874
var myProp = 'prop';
if(myObj.hasOwnProperty(myProp)){
alert("yes, i have that property");
}
Or
或
var myProp = 'prop';
if(myProp in myObj){
alert("yes, i have that property");
Or
或
if('prop' in myObj){
alert("yes, i have that property");
}
#2
30
You can use hasOwnProperty, but based on the reference you need quotes when using this method:
您可以使用hasOwnProperty,但是在使用此方法时需要引用引用:
if (myObj.hasOwnProperty('myProp')) {
// do something
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
Another way is to use in operator, but you need quotes here as well:
另一种方法是在操作符中使用,但这里也需要引号:
if ('myProp' in myObj) {
// do something
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
#3
19
Thank you for everyone's assistance and pushing to get rid of the eval statement. Variables needed to be in brackets, not dot notation. This works and is clean, proper code.
感谢大家对eval语句的帮助和推动。变量需要放在括号中,而不是点符号。这是可行的,并且是干净的,正确的代码。
Each of these are variables: appChoice, underI, underObstr.
每一个都是变量:appChoice, underI, underObstr。
if(typeof tData.tonicdata[appChoice][underI][underObstr] !== "undefined"){
//enter code here
}
#4
9
A much more secure way to check if property exists on the object is to use empty object or object prototype to call hasOwnProperty()
检查对象上是否存在属性的更安全的方法是使用空对象或对象原型来调用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
Reference from MDN Web Docs - Object.prototype.hasOwnProperty()
来自MDN网络文档的引用- Object.prototype.hasOwnProperty()
#5
3
For own property :
为自己的财产:
var loan = { amount: 150 };
if(Object.prototype.hasOwnProperty.call(loan, "amount"))
{
//will execute
}
Note: using Object.prototype.hasOwnProperty is better than loan.hasOwnProperty(..), in case a custom hasOwnProperty is defined in the prototype chain (which is not the case here), like
注意:使用Object.prototype。hasOwnProperty比loan.hasOwnProperty(..)要好,如果在prototype链中定义了定制的hasOwnProperty(这里不是这样),比如
var foo = {
hasOwnProperty: function() {
return false;
},
bar: 'Here be dragons'
};
/ / https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
To include inherited properties in the finding use the in operator: (but you must place an object at the right side of 'in', primitive values will throw error, e.g. 'length' in 'home' will throw error, but 'length' in new String('home') won't)
要在查找中包含继承的属性,请使用in运算符:(但是必须将对象放置在“in”的右边,原始值将抛出错误,例如:“home”中的“length”会出错,而“length”在新字符串(“home”)中不会出错。
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
/ / https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
Note: One may be tempted to use typeof and [ ] property accessor as the following code which doesn't work ...
注意:您可能想要使用typeof和[]属性访问器,因为下面的代码不起作用……
var loan = { amount: 150 };
loan.installment = undefined;
if("installment" in loan) // correct
{
// will execute
}
if(typeof loan["installment"] !== "undefined") // incorrect
{
// will not execute
}
#6
-1
You can use hasOwnProperty()
as well as in
operator.
可以使用hasOwnProperty()和in运算符。
#7
-2
You can also use this object-hasOwnProperty component I wrote to avoid copy-pasting it across places. It can be used to determine whether the object has the specified property.
您还可以使用我编写的object-hasOwnProperty组件,以避免在各处复制粘贴它。它可以用来确定对象是否具有指定的属性。
Examples:
例子:
hasOwnProperty({foo: 'bar'}, 'foo') // => true
hasOwnProperty({foo: 'bar'}, 'bar') // => false
How it works:
它是如何工作的:
function hasOwnProperty(obj: {}, prop: string|number): boolean {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
#8
-9
what about? !!myObject['myProp']
works for me.
关于什么?! !myObject myProp的适合我。
#9
-11
if(myObj[myProp])
{
`enter code here`
}