开发者

How do I get a property of an object by string?

I have an object, in an array. Doing this:

alert(myObject.cats[1].nickname);

will output 'fluffykins' or whatever t开发者_C百科he nickname is.

Is there a way to access this property by string? ie.

var param = 'nickname';
alert(myObject.cats[1].{param});


Yes, just use square brackets as usual.

myObject.cats[1][param]


You can do this:

var test = { 'prop1': 'test' };

alert(test['prop1']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜