Created
September 25, 2017 06:38
-
-
Save Alexgalinier/b068a7f0730b339f078b434ea6ac5647 to your computer and use it in GitHub Desktop.
Filter an array of people by age and group them by gender.
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
function filterAndGroup(data, filterFunc, groupByKey) { | |
return data.reduce((acc, item) => { | |
if (filterFunc(item)) { | |
if (item[groupByKey] in acc) { | |
acc[item[groupByKey]].push(item); | |
} else { | |
acc[item[groupByKey]] = [item]; | |
} | |
} | |
return acc; | |
}, {}); | |
} | |
function filterBy30to40AndGroupByGender(data) { | |
return filterAndGroup( | |
data, | |
(_) => _.age > 30 && _.age < 40, | |
'gender' | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment