Created
January 12, 2018 23:39
-
-
Save guillaumegarcia13/668518119667594fdca150ebefecd194 to your computer and use it in GitHub Desktop.
groupBy in Typescript (for use in Angular)
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
declare global { | |
interface Array<T> { | |
groupBy(prop: T): Array<T>; | |
} | |
} | |
if (!Array.prototype.groupBy) { | |
Array.prototype.groupBy = (prop: string) => { | |
return this.reduce(function(groups, item) { | |
const val = item[prop]; | |
groups[val] = groups[val] || []; | |
groups[val].push(item); | |
return groups; | |
}, {}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment