Click here to Skip to main content
15,441,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to learn nodejs and trying to run some script to

using the below package

https://github.com/harpreetkhalsagtbit/country-state-city

to get iso codes by inputting country names?

// Latest version - v3.0.0
let Country = require('country-state-city').Country;
let State = require('country-state-city').State;

console.log(Country.getAllCountries())


the above fetches me al the countries along with iso code .

I want to input county name and get only iso code of that country.

const inputs = Country.getAllCountries();
//to get all the countries in an array of objects and put them in input variable

// sample array that we ionsert in-->
[{
isoCode: 'HU',
name: 'Hunguray',
phonecode: '36'
},
{
isocode: 'IS',
name: 'Iceland',
phonecode: '354'
}
]{
inputs.forEach(input=>{
for(let key in input){
console.log(`$(key) : ${input[key]}`);
}
});
How do i get only iso code ?


What I have tried:

I am trying to learn nodejs and trying to run some script to

using the below package

https://github.com/harpreetkhalsagtbit/country-state-city

to get iso codes by inputting country names?

// Latest version - v3.0.0
let Country = require('country-state-city').Country;
let State = require('country-state-city').State;

console.log(Country.getAllCountries())


the above fetches me al the countries along with iso code .

I want to input county name and get only iso code of that country.

const inputs = Country.getAllCountries();
//to get all the countries in an array of objects and put them in input variable

// sample array that we ionsert in-->
[{
isoCode: 'HU',
name: 'Hunguray',
phonecode: '36'
},
{
isocode: 'IS',
name: 'Iceland',
phonecode: '354'
}
]{
inputs.forEach(input=>{
for(let key in input){
console.log(`$(key) : ${input[key]}`);
}
});
How do i get only iso code ?
Posted
Updated 28-Feb-22 22:31pm

1 solution

You'll need to do two things: 1) search for a country matching the name provided; 2) map the resulting country to the ISO code only. The simplest way:

JavaScript
const matches = countries.filter(e => e.name == '<name>');

if (matches.length > 0) {
  const country = matches[0];
  console.log(country.isoCode);
}

The filter() method loops through all entries in the array and performs a match based on the name. Then we simply check the number of matches and select the first one.

This doesn't factor in things like case-sensitivity in the country name, or account for when there's more than one result (there shouldn't be).
 
Share this answer
 
Comments
harsha 4 1-Mar-22 4:49am     CRLF
thanks for the approach but if i want to get a iso codes for a bunch of countries, is there a way apart from putting country name everytime?
Chris Copeland 1-Mar-22 4:57am     CRLF
If what you want is "ISO code by country name" then yes you would need to provide a country name every time you wanted an ISO code. If you're reading the country name from an input then you can simply use the input value directly in the filter() method. Otherwise, the alternative would be to map all of the countries to an object like so: const countriesByName = {}; countries.forEach(c => countriesByName[c.name] = c.isoCode); Then accessing the object direct via the country name would give you the ISO code: console.log(countriesByName['Iceland']);
harsha 4 1-Mar-22 6:04am     CRLF
const newList = Object.keys(countries) // get all the countries in an array is there a way to get the code for all the ones in the new array which has lot of countries? just curious
Chris Copeland 1-Mar-22 8:39am    
Consider using map()[^] which is used to convert an array of values into another array. The documentation on there is pretty comprehensive so you can use it to work out other things. A lot of what you're asking is basic coding questions so it might be worth reading up and looking at some examples!
harsha 4 1-Mar-22 8:47am    
thanks chris

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900