Skip to content

Instantly share code, notes, and snippets.

@louisebolden
Last active April 13, 2022 11:18
Show Gist options
  • Save louisebolden/3cb1db65888aae87bde99fd53226e28d to your computer and use it in GitHub Desktop.
Save louisebolden/3cb1db65888aae87bde99fd53226e28d to your computer and use it in GitHub Desktop.
Get a JSON array of cities and countries from Teleport.
$.get('https://api.teleport.org/api/countries/').then(function(data) {
var countries = $.map(data['_links']['country:items'], function(country) { return country.name });
fetchedCities = [];
countries.forEach(function(country) {
$.get('https://api.teleport.org/api/cities/?search=' + country + '&embed=city%3Asearch-results%2Fcity%3Aitem%2Fcity').then(function(data) {
var cities = data['_embedded']['city:search-results'];
cities.forEach(function(city) {
var city = {
country: country,
fullName: city['_embedded']['city:item'].full_name,
name: city['_embedded']['city:item'].name,
population: city['_embedded']['city:item'].population
}
fetchedCities.push(city);
});
console.log(fetchedCities);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment