Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  7] [ 4]  / answers: 1 / hits: 45816  / 13 Years ago, tue, april 12, 2011, 12:00:00

How can I use the value of the variable a as a key to lookup a property? I want to be able to say: b[whatever] and have this return 20:



var a = whatever;
var b = {a : 20}; // Want this to assign b.whatever
alert(b[whatever]); // so that this shows 20, not `undefined`


I am asking if it's possible during the creation of b, to have it contain whatever:20 instead of a:20 where whatever is itself in a variable. Maybe an eval could be used?


More From » dictionary

 Answers
37
var a = whatever;
var b = {};
b[a] = 20;
alert(b[whatever]); // shows 20

[#92776] Monday, April 11, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maurodannyr

Total Points: 126
Total Questions: 103
Total Answers: 105

Location: Maldives
Member since Sun, Feb 27, 2022
2 Years ago
;