Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda