Skip to content

Instantly share code, notes, and snippets.

@jamsea
Created April 17, 2018 07:09
Show Gist options
  • Save jamsea/ac118a3dbd0bd609e195e3774e63ac02 to your computer and use it in GitHub Desktop.
Save jamsea/ac118a3dbd0bd609e195e3774e63ac02 to your computer and use it in GitHub Desktop.
City Sort (Typescript)
interface ICity {
[key: string]: number;
}
const cities: ICity[] = [
{ "San Francisco": 3000 },
{ "Los Angeles": 5000 },
{ "San Francisco": 3000 },
{ Sacramento: 2000 }
];
const uniqueCityFilter = (cityObject: ICity, index: number, array: ICity[]) => {
return (
index ===
array.findIndex(s => {
const cityName = Object.keys(s)[0];
const currentCityName = Object.keys(cityObject)[0];
return cityName === currentCityName;
})
);
};
const sumCityPopulation = (population: number, currentCity: ICity) => {
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