Access to the properties of an object where the property name is in a variable

I am trying to check if any element exists in a JS object. To do this, I need to use any identifier passed to my method. I am currently struggling to use the value of a variable. That's what I'm doing:

data.entries.id

So, I have an object setup like:

var data = { 
  "entries" : { 
  }
};

Therefore, the part .idchecks for a specific identifier. If so, I do nothing; if it is not, I want to add it. At the moment, using data.entries.id, every time I just check if the "id" exists in the "records", which I don't want. Say my variable value was "part1", but instead data.entries.idI want it to be searched data.entries.part1.

So, how do I pass the value of a variable when I check this, as opposed to the name of the variable.

I hope this makes sense, and I hope you can help!

+2
source share
1 answer

Use data.entries[id]; // Where id is a variable

This designation is access to a property using a variable.

+2
source

All Articles