0

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 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

but how would you do that in a variable dimension array?
@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]].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.