Meres
Meres

Reputation: 21

how to use variable in a variable name

so i am working with a json variable like this:

opponentInvData.item1

it contains items 1 through 6

i need to access the different items dynamically and set them to null. itemNum is the specific item i need to access. im trying to use the eval function

var itemNum = 2;
eval(opponentInvData.item + itemNum + ' = ""');

needless to say, it's not working, any ideas?

Upvotes: 2

Views: 236

Answers (1)

Nikita Rybak
Nikita Rybak

Reputation: 68006

opponentInvData['item1'] = "my item"

is the same as

opponentInvData.item1 = "my item"

Upvotes: 6

Related Questions