Skip to content
Advertisement

Mapping through multiple possible values in an object – React component

I currently have a component that takes a currencyCode and returns an SVG of the corresponding country. I want to expand the component for instances whereby we want to search by country name and not by currency code. The current props that are passed into the component are:

currencyCode – which is something like “AED” & countryLabel – which is something like “United Arab Emirates”

JavaScript

I tried to update my currencyCodeMap to something like:

AED | "United Arab Emirates" so that either the label or the code would return a flag, but no joy. Any suggestions?

Advertisement

Answer

AED | "United Arab Emirates" is not valid JavaScript syntax.

Why not have an object like:

JavaScript

Then have an array of these and use .find() to get the component.

It would look something like this:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement