I have
object.name = 'foo';
var value = 'bar';
...
var params = { object.name : 1, value : value};
I want a result equivalent to
var params = { foo : 1, value : bar };
what can I do?
I have
object.name = 'foo';
var value = 'bar';
...
var params = { object.name : 1, value : value};
I want a result equivalent to
var params = { foo : 1, value : bar };
what can I do?
Objects are sometimes called associative arrays
, since each property is associated with a `string value that can be used to access it.
Try using []
to set object property -
object.name = 'foo';
var value = 'bar';
var params = { value:value};
params[ object.name] = 1;
Output:- {value: "bar", foo: 1}