mercredi 28 juin 2017

javascript: avoding if else with javascript object when pattern matching is involved

I am basically trying to avoid branching. I will explain my doubt with the code:

My code is like:

name = "any_random_name_generated_dynamically"

//If else conditions
// foo and bar are the part of the name not the complete name

if (name.match('foo_pattern')) {
"do_thing_1"
}
else if (name.match('bar_pattern')) {
"do_thing_2"
}
else {
"do_thing_3"
}

Is there anyway I can avoid this using javascript objects? Or there any other way to do where less branching is involved.

I tried googling it bu didn't find anything for this particular issue.

Aucun commentaire:

Enregistrer un commentaire