So I have an array of countries names abbreviated am trying to get and display the full country's name in an object.
const countries = [
{
name:'Argentina',
border:["NGA", "PAK","CHN"],
},
{
name:'China',
border:["ARG", "PAK"],
},
{
name:'Nigeria',
border:['PAK', 'CHN'],
},
{
name:'Pakistan',
border:['NGA', 'ARG'],
},
]
so if i access the first index of the array which the name is Argentina and border of nga, pak and chn
const country = countries.find((con) => con.name === "Argentina");
console.log(country.border);
});
so am only seeing Abbr values in the console just as expected which is this
[NGA, PAK, CHN]
but I actually want something like this
[Nigeria, Pakistan, China]
So how do I actually log country's fullName to the console with the objects provided because have thought and thought of a way to do it, but am just not getting the idea data-URL-link
You can first create a dict as
fetch("https://restcountries.eu/rest/v2/all")
.then((res) => res.json())
.then((data) => {
const dict = data.reduce((acc, curr) => {
acc[curr.alpha3Code] = curr.name;
return acc;
}, {});
console.log(dict);
});
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
const dict = {
NGA: "Nigeria",
PAK: "Pakistan",
CHN: "China",
ARG: "Argentina",
};
const countries = [
{
name: "Argentina",
border: ["NGA", "PAK", "CHN"],
},
{
name: "China",
border: ["ARG", "PAK"],
},
{
name: "Nigeria",
border: ["PAK", "CHN"],
},
{
name: "Pakistan",
border: ["NGA", "ARG"],
},
];
const result = countries.map((o) => dict[o.border[0]]);
console.log(result);
You can map the results by a function which changes abbrs into full names.
const countries = [
{
name:'Argentina',
border:["NGA", "PAK","CHN"],
},
{
name:'China',
border:["ARG", "PAK"],
},
{
name:'Nigeria',
border:['PAK', 'CHN'],
},
{
name:'Pakistan',
border:['NGA', 'ARG'],
},
]
const abrRefrence = {
CHN: "China",
PAK: "Pakistan",
NGA: "Nigeria",
ARG: "Argentina",
}
const abrToFull = abr => abrRefrence[abr];
const country = countries.find(con => con.name === "Argentina");
console.log(country.border.map(abrToFull));