I have an object:
let commands = {
help: () => consoleDialog('defaultMessage'),
ls: () => consoleDialog('defaultMessage2')
};
How can I use an argument as a key identifier to invoke the function held in value:
function changeText(currentValue) {
commands.currentValue()
}
In the example above, if currentValue is equal to help, then commands.help() should be executed.
>Solution :
You can use it like..
let commands = {
help: () => console.log("defaultMessage"),
ls: () => console.log("defaultMessage2")
};
function changeText(currentValue) {
// takes the property of the object dynamically
commands[currentValue]();
}
changeText("help");