Last active
April 17, 2018 07:09
-
-
Save jamsea/b765b7b3ab7705844f6a3864bf7c27a5 to your computer and use it in GitHub Desktop.
City Sort
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
const cities = [ | |
{ "San Francisco": 3000 }, | |
{ "Los Angeles": 5000 }, | |
{ "San Francisco": 3000 }, | |
{ Sacramento: 2000 } | |
]; | |
const uniqueCityFilter = (cityObject, index, array) => { | |
return ( | |
index === | |
array.findIndex(s => { | |
const cityName = Object.keys(s)[0]; | |
const currentCityName = Object.keys(cityObject)[0]; | |
return cityName === currentCityName; | |
}) | |
); | |
}; | |
const sumCityPopulation = (population, currentCity, cityArray) => { | |
const cityPopulation = Object.values(currentCity)[0]; | |
return population + cityPopulation; | |
}; | |
const total = cities.filter(uniqueCityFilter).reduce(sumCityPopulation, 0); | |
console.log(total); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment