217

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'
}
3

8 Answers 8

219

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',
}
2
  • 3
    jsonVariable = {}; should not be initialized inside the for loop.
    – closure
    Commented Dec 12, 2012 at 5:15
  • Also you're making jsonVariable global here Commented Dec 12, 2012 at 5:17
210
var jsonVariable = {};
for(var i=1; i < 3; i++) {
  jsonVariable[i + 'name'] = 'name' + i;        
}
3
  • 5
    Musa's response below also correctly articulates that using [] notation is actually the only way to do it. Commented Dec 12, 2012 at 5:19
  • 1
    Also note that @ChilNut's response below now describes the easiest way of doing this using ES6 Commented Jan 28, 2016 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
    Commented Jan 28, 2016 at 17:36
146

With ES 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.

1
  • Without ES6 I can't define var payload={dynamicKey:val} , I need to do var payload={} and payload[variableName]=val. Commented Apr 2, 2018 at 11:36
42

Use a variable as an object key

let key = 'myKey';

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

See How to iterete on your object here

2
  • 1
    Can you please elaborate a little more to let others know why this might be a solution?
    – phaberest
    Commented May 2, 2019 at 15:47
  • The info you added are fine, just never give an answer with just code ;)
    – phaberest
    Commented Sep 16, 2019 at 16:09
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;
}
0
8

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'
}
1
  • This works nice, I wonder if it works in all browsers / versions
    – Mercury
    Commented Dec 20, 2015 at 15:38
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'
}
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;

Not the answer you're looking for? Browse other questions tagged or ask your own question.