B
B
Banan442020-04-16 10:22:29
JavaScript
Banan44, 2020-04-16 10:22:29

How to get a property from a string by name?

const elements = {
    attributes: {
        text: "Привет"
    }
}

function getKey(key){
    return elements.attributes.key;
}
let value = getKey("text"); // elements.attributes.text - "Привет"


Can this be done without using eval()? If possible, how?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-04-16
@Banan44

function getKey(key){
    return elements.attributes[key];   // <- так
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question