Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get country name from ISO code in Javascript [duplicate]

Having the list of country ISO codes, they are available here for reference:

Is there a way to return the country name from it?

Like, having a function, getCountryName(), which if called getCountryName('AL') will return 'Albania' and so on.

I was doing it as saving the whole list and work on it as with a dictionary but I was wondering if there is a method without saving the whole countries into a list.

like image 803
Jean Pierre Avatar asked Sep 15 '25 22:09

Jean Pierre


2 Answers

You can try Intl.DisplayNames() of ECMAScript Internationalization API Intl:

var getCountryNames = new Intl.DisplayNames(['en'], {type: 'region'});
console.log(getCountryNames.of('AL'));  // "Albania"
like image 179
Mamun Avatar answered Sep 17 '25 13:09

Mamun


Hei you could use the JSON version of what you need, here https://pkgstore.datahub.io/core/country-list/data_json/data/8c458f2d15d9f2119654b29ede6e45b8/data_json.json

then, with a forach, you loop over the file, with your ISO code, and when you find it, you ask your script to return the state name

like image 30
noxter Avatar answered Sep 17 '25 11:09

noxter