NOT duplicate of : Dynamically access object property using variable
How to read the JavaScript Object Array property dynamically.
var person = {
name: "Ravi",
age: 25
friends: [{
name: "Suresh"
},
{
name: "Nitin"
},
{
name: "Argha"
}
]
}
So, if I want to read any property dynamically, I can use
var dynamicProperty = 'age';
person[dynamicProperty] // Output : 25
But it fails for array.
var dynamicProperty = 'friends[1]';
person[dynamicProperty].name // Output : undefined
What is the best way to pass the name of the array dynamically ?