开发者

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

开发者 https://www.devze.com 2023-02-16 14:46 出处:网络
I have an object, in an array. Doing this: alert(myObject.cats[1].nickname); will output \'fluffykins\' or whatever t开发者_C百科he nickname is.

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

精彩评论

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