Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  69] [ 1]  / answers: 1 / hits: 19165  / 11 Years ago, wed, july 24, 2013, 12:00:00

I would expect the following three associative arrays to be identical:



arr1 = { dynamic:foo, bar:baz };

key = dynamic;
arr2 = { key:foo, bar:baz };

arr3 = {};
arr3[key] = foo;
arr3[bar] = baz;


In the above examples, arr1 and arr3 are the same, but arr2 is different.



Is it possible to use dynamic keys in the declaration of a javascript associative array?


More From » javascript

 Answers
63

It is now possible to use dynamic keys in the declaration of a javascript object, in any browser/platform that supports ES6 literal shorthands:



key = dynamic;    
arr2 = {
[key]: foo, // dynamic: foo
bar: baz
};

[#76771] Tuesday, July 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arthur

Total Points: 729
Total Questions: 107
Total Answers: 109

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;