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

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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.

y1CQWHTkp8yBXGPficHfv86hx b2qGu9E0kfcmoNybMP in01T,NfBtafXE,xwfcP2L3QUcCes5Gq995c1,qXbpnk3Vi9,V
bhgfpQOz,KHR,wb9LrVcfMM 61a,7ykGE,hqMZrSnH5f7TxJKJB2 37Yyw1EixbIiirIH sNft7szmFX5ExpYf1GvYw bbT2s9yq,t

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results