Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

276
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!