Skip to content

Instantly share code, notes, and snippets.

@jamsea
Last active April 17, 2018 07:09
Show Gist options
  • Save jamsea/b765b7b3ab7705844f6a3864bf7c27a5 to your computer and use it in GitHub Desktop.
Save jamsea/b765b7b3ab7705844f6a3864bf7c27a5 to your computer and use it in GitHub Desktop.
City Sort
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