Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
get full country name from country abbreviations in Javascript

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

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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);

about 4 years ago · Juan Pablo Isaza Relatório

0

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));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda