Created
April 17, 2018 07:09
-
-
Save jamsea/ac118a3dbd0bd609e195e3774e63ac02 to your computer and use it in GitHub Desktop.
City Sort (Typescript)
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
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