I want to call different functions depending on the input I find. If the input matches one of the keys, I want to call the corresponding function - with some parameters:
var commands = {
'key1': someFunction,
'key2': otherFunction
};
The keys are strings and to make it easier I created the command_keys variable:
var command_keys = Object.keys(commands);
I define the two functions further down:
function someFunction(param) {
// do sth.
};
function otherFunction(param) {
// do sth. else
};
Then I have a condition checking for the key and calling one of the functions depending on what key I found:
if (command_keys.indexOf(some_string) > -1) {
index = command_keys.indexOf(some_string);
commands[index](some_param);
}
However, I get an error:
Uncaught TypeError: commands[index] is not a function(anonymous function)
Thank you for your ideas.
some_array? It would be better if you show us the full script.command_keysis an array, thenindexis an integer andcommandsdoes not have integer keys.