Created
June 6, 2021 11:09
-
-
Save dmorenogogoleva/11ce687445f9f78ff67da6ffe7c8dc9e to your computer and use it in GitHub Desktop.
custom groupBy
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
export function groupBy(collection: any[], iteratee: string) { | |
return collection.reduce((result, value) => { | |
if (!value.hasOwnProperty(iteratee)) return result | |
const key = value[iteratee] | |
if (!result || !result[key]) { | |
result[key] = [value] | |
return result | |
} | |
result[key] = [...result[key], value] | |
return result | |
}, {}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment