Reputation:
I have an array of continents and countries:
const continents = [
{
continent: 'Europe',
countries: [
'Albania',
'Andorra',...
],
},
{
continent: 'Asia',
countries: [
'Afghanistan',
'Bahrain',...
],
},
{
continent: 'Africa',
countries: [
'Algeria',
'Angola',...
],
},
{
continent: 'Americas',
countries: [
'Antigua and Barbuda',
'Bahamas',...
],
},
]
And an array of some countries:
const countries = ['Angola', 'Andorra']
I want to iterate over countries and return an array of objects like that:
const result = countries.map(country => {
return {
continent: ??,
country: country,
}
})
How can I find the continent of each countries?
I try this:
const continent = continents.forEach((continent, i) => {
const countries = continent.countries
if (find(countries, value.country)) return continent.continent
})
But I get
[ts] Not all code paths return a value. [7030]
Upvotes: 3
Views: 2367
Reputation: 22876
For multiple lookups, more efficient approach would be to create a lookup object :
const lookup = {}, countries = ['Angola', 'Andorra'], continents = [ { continent: 'Europe', countries: [ 'Albania', 'Andorra' ], }, { continent: 'Asia', countries: [ 'Afghanistan', 'Bahrain' ], }, { continent: 'Africa', countries: [ 'Algeria', 'Angola' ], }, { continent: 'Americas', countries: [ 'Antigua and Barbuda', 'Bahamas' ] } ];
for (let obj of continents)
for (let country of obj.countries)
lookup[country] = obj.continent;
const result = countries.map(country => ({ country, continent: lookup[country] }));
console.log( result );
console.log( lookup );
Upvotes: 0
Reputation: 1475
First reduce the continents array to an object with key as continent and value as the countries array.
let continentObj = continents.reduce((obj,currentObj) => {
obj[currentObj.continent] = currentObj.countries
return obj
},{})
Then get the continent by running a map on countries and find the continent inside the object created in the reduce step.
const countryObj = countries.map(country => {
const continent = Object.keys(continentObj).find(continentName => continentObj[continentName].indexOf(country) > -1)
return {
continent,
country
}
})
console.log(countryObj)
Upvotes: 0
Reputation: 37755
You can use reduce and includes.
So here the idea is loop through the continent
array and filter the countries which are available in countries array. Than map through the matchingCountry
and create a object with desired keys and push in final output.
const continents = [{ continent:'Europe',countries: ['Albania','Andorra',],},{continent: 'Asia',countries: ['Afghanistan', 'Bahrain', ], },{continent: 'Africa',countries: ['Algeria', 'Angola',],},{continent: 'Americas',countries: [ 'Antigua and Barbuda','Bahamas',],},]
const countries = ['Angola','Andorra', 'Andorra', 'Bahamas']
const output = continents.reduce((op,cur) => {
let matchCounrty = cur.countries.filter(e => countries.includes(e) )
if( matchCounrty && matchCounrty.length ){
let desiredObject = matchCounrty.map(e => ({
continent: cur.continent,
country: e
}))
op.push(desiredObject)
}
return op
},[])
console.log(output)
Upvotes: 0
Reputation: 36584
You need to map map
each country name country name to newObj. And then you can use find()
method on continents
to find out whether the countries
array of that continent obj contains the country name or not
const continents = [
{
continent: 'Europe',
countries: [
'Albania',
'Andorra'
],
},
{
continent: 'Asia',
countries: [
'Afghanistan',
'Bahrain'
],
},
{
continent: 'Africa',
countries: [
'Algeria',
'Angola'
],
},
{
continent: 'Americas',
countries: [
'Antigua and Barbuda',
'Bahamas'
],
},
]
const countries = ['Angola', 'Andorra']
const result = countries.map(count => {
const obj = {country:count};
//get the continent object from array continents which includes 'count' in its countries array
const continentObj = continents.find(continent=>continent.countries.includes(count));
//add continent name to the obj as continent name in continentObj
obj.continent = continentObj.continent;
return obj;
})
console.log(result);
Upvotes: 0
Reputation: 386654
You could find continent
by checking the value with Array#find
and Array#includes
const
continents = [{ continent: 'Europe', countries: ['Albania', 'Andorra'] }, { continent: 'Asia', countries: ['Afghanistan', 'Bahrain'] }, { continent: 'Africa', countries: ['Algeria', 'Angola'] }, { continent: 'Americas', countries: ['Antigua and Barbuda', 'Bahamas'] }],
countries = ['Angola', 'Andorra'],
result = countries.map(country => ({
continent: continents.find(({ countries }) =>
countries.includes(country)).continent,
country,
}));
console.log(result);
Upvotes: 4