Browse Source

Adds groupBy function

main
Eric Amodio 7 years ago
parent
commit
ea6fdbaaf2
1 changed files with 9 additions and 0 deletions
  1. +9
    -0
      src/system/array.ts

+ 9
- 0
src/system/array.ts View File

@ -1,6 +1,15 @@
'use strict';
export namespace Arrays {
export function groupBy<T>(array: T[], accessor: (item: T) => any): T[] {
return array.reduce((previous, current) => {
const value = accessor(current);
previous[value] = previous[value] || [];
previous[value].push(current);
return previous;
}, Object.create(null));
}
export function uniqueBy<T>(array: T[], accessor: (item: T) => any, predicate?: (item: T) => boolean): T[] {
const uniqueValues = Object.create(null);
return array.filter(_ => {

Loading…
Cancel
Save