Skip to content

Instantly share code, notes, and snippets.

@guillaumegarcia13
Created January 12, 2018 23:39
Show Gist options
  • Save guillaumegarcia13/668518119667594fdca150ebefecd194 to your computer and use it in GitHub Desktop.
Save guillaumegarcia13/668518119667594fdca150ebefecd194 to your computer and use it in GitHub Desktop.
groupBy in Typescript (for use in Angular)
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