简体   繁体   English

如何在变量名中使用变量

[英]how to use variable in a variable name

so i am working with a json variable like this: 所以我正在使用这样的json变量:

opponentInvData.item1

it contains items 1 through 6 它包含项目1到6

i need to access the different items dynamically and set them to null. 我需要动态访问不同的项目并将它们设置为null。 itemNum is the specific item i need to access. itemNum是我需要访问的特定项目。 im trying to use the eval function 我正在尝试使用eval函数

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

needless to say, it's not working, any ideas? 不用说,这是行不通的,有什么主意吗?

opponentInvData['item1'] = "my item"

是相同的

opponentInvData.item1 = "my item"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM