So I'm trying access the property named all like this
let lang = organizationLocales[0].locale;
alertDetails.alertMessage.`${lang}`.all
But sadly, I'm getting an error near that dollar symbol saying that "identifier expected", I just want to access the property in a dynamic way as the variable lang will be changing based on the user input. I'd appreciate any help, thanks in advance.
P.S organizationLocales is an array that has nothing to do with alertDetails object.
CodePudding user response:
You can access to the property of an object through the index accessor []
. So if you want to use a string to address a property, you should enclose such string inside square brackets.
You have to think of it like an associative array where each property name is paired to its value, including functions. Since in js syntax, the dot expects the property name statically defined, you cannot compose an identifier like that. But yet you can use a different way to get there without relying on dot.
To better answer to your specific problem, this may be the solution:
let lang = organizationLocales[0].locale;
alertDetails.alertMessage[lang].all