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']);
精彩评论