How to dynamically access value of a property in Javascript object? [duplicate]


How to dynamically access value of a property in Javascript object? [duplicate]
This question already has an answer here:
I have following JS object:-
var attributes = {
entityData: {
Party: 12
},
entityType: "Party"
};
Now i want to fetch dynamically the Party property value something like below, how can i do this?
alert(attributes.entityData.{attributes.entityType});
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2 Answers
2
whenever you need to access dynamic property you have to use square bracket for accessing property .
Syntax: object[propery}
var attributes = {
entityData: {
Party: 12
},
entityType: "Party"
};
alert(attributes.entityData[attributes.entityType]);
alert(attributes.entityData[attributes.entityType]);
You want something like this perhaps:
var attributes = {
entityData: {
Party: 12
},
entityType: "Party"
};
alert(attributes.entityData[attributes['entityType']]);
Use square braces instead of curly braces.