Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  169] [ 7]  / answers: 1 / hits: 132672  / 13 Years ago, wed, august 3, 2011, 12:00:00

How to access an object using a variable as key. Here is my code sample:



var o = {k1: 111, k2: 222};
alert(o.k1); //working fine
var key = k+1; alert(key); // k1
alert(o.key); //not working

More From » javascript

 Answers
23

You can access objects like arrays:



alert(o[key]);

[#90853] Monday, August 1, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lizet

Total Points: 479
Total Questions: 96
Total Answers: 113

Location: Mexico
Member since Sun, Jul 25, 2021
3 Years ago
;