views:

69

answers:

1

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 only answer I could find is:

newVal = 4;

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

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

+1  A: 

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;
Guffa
but how would you do that in a variable dimension array?
djspark
@djspark: As they are not multidimensional arrays but only arrays in arrays, you can do it in a loop: var p = productOptionTree; for (var i=0;i<product.length-1;i++) p = p[product[i]]; Now you access the item using p[product[product.length-1]].
Guffa