2

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

So I have my object setup as:

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

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!

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Creights
  • 907
  • 1
  • 12
  • 25

1 Answers1

2

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

This notation is to access the property using a variable

Prasath K
  • 4,950
  • 7
  • 23
  • 35
  • I knew it was something easy that I was overlooking, thanks - just waiting 5 mins for stack overflow to allow me to accept the answer. – Creights May 07 '13 at 11:38