How to get a property from a string by name?

const elements = {
 attributes: {
 text: "Hello"
}
}

function getKey(key){
 return elements.attributes.key;
}
let value = getKey("text"); // elements.attributes.text - "Hello"


Is it possible to implement this without using eval()? If so, how?
April 19th 20 at 12:16
1 answer
April 19th 20 at 12:18
Solution
function getKey(key){
 return elements.attributes[key]; // <- wrong
}
Thanks, I forgot that the properties can be invoked as follows (using [ ]) - camden_Jacobi commented on April 19th 20 at 12:21

Find more questions by tags JavaScript