0

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.

9
  • What's some_array? It would be better if you show us the full script. Commented Aug 8, 2015 at 21:25
  • 2
    So if command_keys is an array, then index is an integer and commands does not have integer keys. Commented Aug 8, 2015 at 21:30
  • @zerkms command_keys is just the keys of commands, e.g. Object.keys(commands); I added this to the questions. Right, the keys of commands are strings and some_array is an array of strings. Commented Aug 8, 2015 at 21:41
  • @PraveenKumar The keys of commands are strings and some_array is an array of strings. Adding this to the question. Commented Aug 8, 2015 at 21:42
  • 1
    @Randomtheories so, check my second comment once again then, it still explains your mistake. It makes sense if you explain what you want to achieve, even though your current code is helpful to understand the problem it does not shed any light on the original aim. Commented Aug 8, 2015 at 21:47

1 Answer 1

3

I don't think you need command_keys at all. commands alone will suffice for what you describe here.

var commands = {
    'key1': someFunction,
    'key2': otherFunction
};

var command = commands[some_string];
if (typeof command === 'function') {
    command(some_param);
}
Sign up to request clarification or add additional context in comments.

4 Comments

What if command_keys is some sort of filter of applicable commands in a given context? Like, we have an edit command but we cannot perform it when something is in a read-only mode.
Well then, you could keep it around and add another condition to the if typeof command === 'function' && command_keys.indexOf(some_string) !== -1. command_keys is still not useful for retrieving the actual command from commands based on some_string, though.
That's right. My point was that you cannot put the "I don't think you need command_keys at all" statement unless some more details provided. PS: oh, I just noticed var command_keys = Object.keys(commands);, so seems right it's indeed redundant.
@NoahFreitas. Thanks for pointing this out. I solved the problem thanks to zerkms as I used the index instead of the key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.