Bladeren bron

Adds groupBy function

main
Eric Amodio 7 jaren geleden
bovenliggende
commit
ea6fdbaaf2
1 gewijzigde bestanden met toevoegingen van 9 en 0 verwijderingen
  1. +9
    -0
      src/system/array.ts

+ 9
- 0
src/system/array.ts Bestand weergeven

@ -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(_ => {

Laden…
Annuleren
Opslaan