Reputation: 907
Depending on the user input ("0", "1", "2") I want to execute a method. What is a good way to do it without
if(input == "0")
methodA();
else if(input == "1")
methodB();
etc..
I rather want to fill a map or something equal like this:
"0" -> methodA()
"1" -> methodB()
"2" -> methodC()
find user input in map and execute the corresponding method.
Upvotes: 0
Views: 62
Reputation: 57036
const methods = {
'0': x => console.log(`method A your input value was ${x}`),
'1': x => console.log(`method B your input value was ${x}`)
}
methods['0'](5)
methods['1'](3)
Upvotes: 0
Reputation: 10078
You can have something like this:
methodA = function () {
Console.log("A");
};
methodB = function () {
Console.log("B");
};
methodC = function () {
Console.log("C");
};
inpResonse = {
"0": this.methodA,
"1": this.methodB,
"2": this.methodC
};
and then invoke (inpResponse[input])()
Upvotes: 1