The problem

I would like to store the key I want to use in a variable then creating my objects like:

var key = "keyOne";
var obj = {key: someValue};

But, when I try to examine the value and I found the key is "key" instead of the value of the variable key. Is there any way to set the value of the key from a variable?


You can use [] to set it.

var key = "keyOne";
var obj = {};
obj[key] = someValue;

References & Resources

  • N/A