In my program, I need to execute different functions based on the input. If the input matches one of the predefined keys, I want to invoke the corresponding function along with some parameters:
var functions = {
'key1': firstFunction,
'key2': secondFunction
};
For easier reference, I created an array of command keys:
var commandKeys = Object.keys(functions);
Further in the code, I define the two functions:
function firstFunction(param) {
// do something
};
function secondFunction(param) {
// do something else
};
Then, I have a condition where I check for the key and call the corresponding function based on the key found:
if (commandkeys.includes(someString)) {
var funcIndex = commandkeys.indexOf(someString);
functions[funcIndex](someParam);
}
However, I encounter an error:
Uncaught TypeError: functions[funcIndex] is not a function(anonymous function)
Any insights or suggestions on how to resolve this issue are appreciated.