Reputation: 7344
i have an object with country codes and country names like this:
countries = {'CA': {name: Canada}, 'GR': {name: Greece}, 'SG': {name: Singapore}}
Then i have another array like this:
markets = ["CA", "GR", "SG"]
Then i want to create an array of objects for every instance of the markets array with with this shape: {label:..., value:....} So i'm writing
const martOptions = markets.map((i) => {
return {label: i, value: i}
})
But for the label i don't want to assign CA or GR. I want somehow to find the name of the country in the countries object and add it to the label so as to have something like this:
{label: Canada, value: CA} and not {label: CA, value: CA}
. Any ideas?
Upvotes: 0
Views: 29
Reputation: 150080
You just need to refer back to the countries
object to get the name associated with the current country code:
const countries = {'CA': {name: 'Canada'}, 'GR': {name: 'Greece'}, 'SG': {name: 'Singapore'}}
const markets = ["CA", "GR", "SG"]
const martOptions = markets.map((i) => ({label: countries[i].name, value: i}))
console.log(martOptions)
Note that countries[i].name
would give you an error if there was a code in the markets
array that didn't exist in the countries
object.
(Note also that if you put it in parentheses you can directly return the new object without needing the { return ... }
structure in the arrow function.)
Upvotes: 4