Acessing javascript multidimensional array with linear values
How could I set a variable that I can read by using eval('productOptionTree' + '[0][1][0]')
?
(the '[0][1][0]' part comes from another variable)
UPDATE it's an ugly question, but I couldn't find another way to do it. the onl开发者_运维技巧y answer I could find is:
newVal = 4;
dim = '[0][1][0]';
eval('productOptionTree'+dim+' = ' +newVal);
You don't need eval to read the item. 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;
精彩评论