PDA

View Full Version : How to dynamically construct javascript object name?



kguha
13 Feb 2011, 9:19 PM
In ext js , lets us say there is a variable like this:
var obj1 = {x: 'x string', y: 'y string'}
If I wish to replace the names 'x' and 'y' with something dynamic e.g:
var v1 = some value
var v2 = some value
and i want:
var obj1 = {result of v1: 'x string', result of v2: 'y string'}
How can I do it?

Thank you.

evant
13 Feb 2011, 9:36 PM
var o = {},
s = 'x';
o.x = 1;
console.log(o.x);
o[s] = 2;
console.log(o.x);