Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic object property names?

How can I make this happen:

var name = otherObject.name; //"string"
var o = { 
            name : otherObject
        };
alert(o["string"].name);
like image 846
Brian Johnson Avatar asked Nov 25 '09 17:11

Brian Johnson


People also ask

What are the dynamic properties?

Dynamic properties of structures characterize a system in form of natural frequencies, damping and mode shape. These dynamic properties are used to formulate mathematical model for its behavior. The evaluation of dynamic properties of is Nnown as modal analysis.

What are the different properties of an object?

Objects have properties that can be observed and described. Physical properties include size, shape, and texture.

How many types of object properties are there?

Objects have two types of properties: data and accessor properties.


1 Answers

Use bracket notation instead.

var name = otherObject.name;
var o = {};
o[name] = otherObject;

Or, in modern JavaScript:

var o = {
  [name]: otherObject,
};
like image 186
Jordan Running Avatar answered Oct 08 '22 20:10

Jordan Running