在属性名称位于变量中的情况下访问对象属性
我正在尝试检查JS对象中是否存在某个项目.为此,我需要使用传递给我的方法的任何ID.目前,我正在努力使用变量的值.这是我在做什么:
I am trying to check if a certain item exists in a JS object. To do this, I need to use whatever ID is passed into my method. At the moment I'm struggling to actually use the variable's value. Here's what I'm doing:
data.entries.id
所以我将对象设置为:
var data = {
"entries" : {
}
};
因此,.id
部分将检查是否存在某个ID.如果可以,我什么都不做,如果不可以,我想添加它.目前,通过使用data.entries.id,每次我只是简单地检查"entries"中是否存在"id",而这并不是我想要的.假设我的变量值是'part1',那么我希望它不是data.entries.id
而是要寻找data.entries.part1
.
Therefore, the .id
part will check if a certain ID exists. If it does, I do nothing, if it doesn't, I want to add it. At the moment, by using data.entries.id, every time I simply check if 'id' exists in 'entries' which is not what I want. Let's say my variable value was 'part1' then instead of data.entries.id
I want it to look for data.entries.part1
.
因此,当我检查变量名时,如何传递变量的值,而不是变量名.
So, how do I pass in the variable's value when I check for this, as opposed to the variable name.
我希望这是有道理的,希望您能提供帮助!
I hope that makes sense, and I hope you can help!
使用data.entries[id]; // Where id is a variable
此符号是使用变量