samedi 21 décembre 2019

How to replace if else ladder

I am having an if-else ladder for a string matching based variable defining. I need to remove the if-else ladder with some other alternatives with some good approaches.

// a is a string
// val is the result

if( a.includes('cars')) {
    val = '4 wheeler';
} else if(a.includes('bikes')) {
    val = '2 wheeler';
} else if(a.includes('bycycle')) {
    val = '2 wheeler type 2'
} else if(a.includes('Truck')) {
    val = 'Multi type vehicle'
}

Aucun commentaire:

Enregistrer un commentaire