212

I am in need to set a JS object property name dynamically.

for(i=1; i<3; i++) {
    var key  = i+'name';

    data = {
        key : 'name1',
    }
}

Result should be:

data = {
    1name: 'name1'
    2name: 'name1'
}
JAAulde
  • 18,787
  • 5
  • 52
  • 61
Vinoth Babu
  • 6,476
  • 10
  • 33
  • 53
  • If they are sequential, why are you not using an Array? – epascarello Dec 12 '12 at 05:20
  • I want to know which three of you hit the wrong arrow. C'mon, fess up. – Charles Dec 12 '12 at 05:26
  • 2
    possible duplicate of [Using a variable for a key in a JavaScript object literal](http://stackoverflow.com/questions/2274242/using-a-variable-for-a-key-in-a-javascript-object-literal) – halfer Mar 10 '15 at 09:43

8 Answers8

211

You'll have to use [] notation to set keys dynamically.

var jsonVariable = {};
for(i=1; i<3; i++) {        
 var jsonKey  = i+'name';
 jsonVariable[jsonKey] = 'name1';

}

Now in ES6 you can use object literal syntax to create object keys dynamically, just wrap the variable in []

var key  = i + 'name';
data = {
    [key] : 'name1',
}
Musa
  • 93,746
  • 17
  • 112
  • 129
208
var jsonVariable = {};
for(var i=1; i < 3; i++) {
  jsonVariable[i + 'name'] = 'name' + i;        
}
closure
  • 7,354
  • 1
  • 21
  • 23
  • 5
    Musa's response below also correctly articulates that using [] notation is actually the only way to do it. – Dennis Plucinik Dec 12 '12 at 05:19
  • 1
    Also note that @ChilNut's response below now describes the easiest way of doing this using ES6 – rambossa Jan 28 '16 at 14:34
  • 1
    Actually, with the new syntax what OP is trying to achieve will only be possible if the loop limit is hardcoded and small. – Musa Jan 28 '16 at 17:36
146

With ECMAScript 6, you can use variable property names with the object literal syntax, like this:

var keyName = 'myKey';
var obj = {
              [keyName]: 1
          };
obj.myKey;//1

This syntax is available in the following newer browsers:

Edge 12+ (No IE support), FF34+, Chrome 44+, Opera 31+, Safari 7.1+

(https://kangax.github.io/compat-table/es6/)

You can add support to older browsers by using a transpiler such as babel. It is easy to transpile an entire project if you are using a module bundler such as rollup or webpack.

chiliNUT
  • 17,890
  • 12
  • 63
  • 99
38

Use a variable as an object key

let key = 'myKey';

let data = {[key] : 'name1'; }

See How to iterete on your object here

BorisD
  • 1,298
  • 12
  • 17
22

This is the way to dynamically set the value

var jsonVariable = {};
for (var i = 1; i < 3; i++) {
    var jsonKey = i + 'name';
    jsonVariable[jsonKey] = 'name' + i;
}
timc
  • 2,044
  • 13
  • 13
7

It does not matter where the variable comes from. Main thing we have one ... Set the variable name between square brackets "[ .. ]".

var optionName = 'nameA';
var JsonVar = {
[optionName] : 'some value'
}
Knut Hering
  • 79
  • 1
  • 2
1
jsonVariable = {}
for(i=1; i<3; i++) {        
   var jsonKey  = i+'name';
   jsonVariable[jsonKey] = 'name1'
}

this will be similar to

    jsonVariable = {
    1name : 'name1'
    2name : 'name1'
}
suman kumar dey
  • 572
  • 1
  • 4
  • 20
0

Along the lines of Sainath S.R's comment above, I was able to set a js object property name from a variable in Google Apps Script (which does not support ES6 yet) by defining the object then defining another key/value outside of the object:

var salesperson = ...

var mailchimpInterests = { 
        "aGroupId": true,
    };

mailchimpInterests[salesperson] = true;