-1

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.

ben
  • 49
  • 8

2 Answers2

1

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");
sms
  • 1,380
  • 1
  • 3
  • 5
0
function currentValue(){return "help"}
let currentValue2 = "help"
function changeText(currentValue) {
    commands[currentValue()]()
    commands[currentValue2]()
}
Edoldin
  • 160
  • 6