Need a little help. I have an array of properties that I filter on. However, if a property does not exist my view does not render because that properties becomes undefined. I need to gracefully ignore or show some other value.
export const countryList = [
{name: 'France', flagCode: 'fr'},
{name: 'England', flagCode: 'england'},
{name: 'Germany', flagCode: 'gm'},
{name: 'default', flagCode: 'default'},
];
const country = `${nationality}`
const flag = countryList.filter(function (el) {
return el.name.toLowerCase() === country.toLowerCase();
})[0].flagCode;
So if ${nationality}
returned "Italy", .flagCode
would return undefined
. I could add Italy to my array to solve this however there will be countries I miss and need a more graceful approached. So In my function I need to check if undefined
, if it is then return a default name of 'default', if it does exist then return the value from the array.
Thanks in advance.