samedi 29 août 2020

Conditionally compare value of array and set it to a variable

So I am trying to set a flag emoji to the flag variable depending on the value of the passed dishes array of objects.
I have a solution, but I bet this can be improved. I mean just imagine 50 countries ...

let flag = ''
const countryFlags = ['🇩🇪', '🇬🇷', '🇪🇸', '🇮🇹']
const dishes = [
   { dish: 'pizza', origin: 'italian' },
   { dish: 'tzatziki', origin: 'greek' }
]

const chooseFlag = a => {
    if (a === 'german') flag = countryFlags[0]
    if (a === 'greek') flag = countryFlags[1]
    if (a === 'spanish') flag = countryFlags[2]
    if (a === 'italian') flag = countryFlags[3]
}

chooseFlag(dishes.origin)

Anybody got a shorthand solution?

Aucun commentaire:

Enregistrer un commentaire