LucianoGanga / country-codes-list

List of codes per country (languages, calling codes, currency codes, etc)
MIT License
94 stars 48 forks source link

How do I filter the country list down? #4

Closed MichaelLiss closed 4 years ago

MichaelLiss commented 4 years ago

Hi,

I am new to node js programming and am trying to use your object:

    var country = 'US';

const myCountryCodesObject = countryCodes.customList('countryCode', '{countryCode} +{countryCallingCode}');
    var indx = myCountryCodesObject.indexOf(country);
    console.log('index ' + indx);

I assume that customList returns a list... and I could do an indexOf that list... but it's not working...

Is there another way that I can get JUST the country code information for ONLY the country I am interested in ?

alexsc6955 commented 4 years ago

@MichaelLiss Sure. You can use the findOne() method to that matter.

const countryCodes = require('country-codes-list')

// Find a country by a property and return the first match as an object
console.log(countryCodes.findOne('countryCode', 'US') )

This will be the output

{
  countryNameEn: 'United States of America',
  countryNameLocal: 'United States of America',
  countryCode: 'US',
  currencyCode: 'USD',
  currencyNameEn: 'United States dollar',
  tinType: '',
  tinName: '',
  officialLanguageCode: 'en',
  officialLanguageNameEn: 'English',
  officialLanguageNameLocal: 'English',
  countryCallingCode: '1',
  region: 'North America',
  flag: '🇺🇸'
}
MichaelLiss commented 4 years ago

AWESOME ! Thank you