Created
December 31, 2018 17:10
-
-
Save lucasjellema/f3aedc9f9985bbfd87867f804d028476 to your computer and use it in GitHub Desktop.
Node JS code to load JSON document with countries and prepare some useful Sets with unique collections of data from the JSON document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var countriesDocumentURL = "https://raw.githubusercontent.com/mledoze/countries/master/countries.json" | |
request(countriesDocumentURL, async function (error, response, body) { | |
var countries = JSON.parse(body) | |
// get unique region values (see: https://codeburst.io/javascript-array-distinct-5edc93501dc4) | |
// take all elements in the countries array, for each of them: take the region element; create a Set of all the resulting region values (a Set contains unique elements) | |
var regions = [...new Set(countries.map(country => country.region))] | |
var subregions = [...new Set(countries.map(country => country.subregion))] | |
// see https://stackoverflow.com/questions/39837678/why-no-array-prototype-flatmap-in-javascript for this flatMap function | |
const flatMap = (f, xs) => | |
xs.reduce((acc, x) => | |
acc.concat(f(x)), []) | |
// take all elements in the countries array, for each of them: take the array of languages ); create one big array of all small arrays of languages (this is what the flatmap does) and turn that big array into a Set (of unique language values) | |
var languages = [...new Set(flatMap(country => Object.values(country.languages), countries))] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment