简体   繁体   English

使用线性值访问JavaScript多维数组

[英]Acessing javascript multidimensional array with linear values

How could I set a variable that I can read by using eval('productOptionTree' + '[0][1][0]') ? 如何设置使用eval('productOptionTree' + '[0][1][0]')可以读取的变量?

(the '[0][1][0]' part comes from another variable) (“ [0] [1] [0]”部分来自另一个变量)


UPDATE it's an ugly question, but I couldn't find another way to do it. 更新这是一个丑陋的问题,但是我找不到另一种方法来做到这一点。 the only answer I could find is: 我唯一能找到的答案是:

newVal = 4; newVal = 4;

dim = '[0][1][0]'; dim ='[0] [1] [0]';

eval('productOptionTree'+dim+' = ' +newVal); eval('productOptionTree'+ dim +'='+ newVal);

You don't need eval to read the item. 您不需要eval即可阅读该项目。 Just do: 做就是了:

var x = productOptionTree[product[0]][product[1]][product[2]];

As you are free of eval, you can now easily use the same way to set the item: 由于您无需评估,因此现在可以轻松地使用相同的方式来设置项目:

productOptionTree[product[0]][product[1]][product[2]] = 42;

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

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