Reputation: 1649
How can I make this happen:
var name = otherObject.name; //"string"
var o = {
name : otherObject
};
alert(o["string"].name);
Upvotes: 16
Views: 11913
Reputation: 106087
Use bracket notation instead.
var name = otherObject.name;
var o = {};
o[name] = otherObject;
Or, in modern JavaScript:
var o = {
[name]: otherObject,
};
Upvotes: 38