I have a simple code like this:
var name = 'lin开发者_开发百科e1';
var obj = {};
obj.name = [0, 1];
console.log(obj);
Key of property is name
. But I want to make key='line'
. Can you help me?
If I understand correctly, and you want to use the value of the name
variable as the property name, you can use this syntax:
obj[name] = [0, 1]; //obj.line1 will be [0, 1]
Object properties can also be accessed with the same syntax arrays use. It is handy in situations like this one.
Try,
obj[name]
or
obj["line1"]
This is known as the bracket notation, and can be used to access any property of an object.
精彩评论