0

How can I create an object with a property which name comes from another variable?

In the example I expected obj to be { foo: 123, nacho: 'tama' } but me is not resolved, it is taken literally.

var me = "nacho"
var obj = { me: "tama" };
// obj is { me: 'tama' } but I expected { nacho: 'tama' }

PS: I know I can it could done in two steps using brackets:

var obj = {};
obj[me] = "tama";

Is is possible to do this at obj creation time?

nacho4d
  • 41,605
  • 42
  • 154
  • 237

0 Answers0