import { Pipe , PipeTransform , NgModule } from '@angular/core' ; import { getProperty , isArray , isUndefined } from '../utils/utils' ; @ Pipe ( { name : 'groupBy' , } ) export class GroupByPipe implements PipeTransform { transform ( input , prop ) { if ( ! isArray ( input ) ) { return input ; } const arr = { } ; for ( const value of input ) { const field = getProperty ( value , prop ) ; if ( isUndefined ( arr [ field ] ) ) { arr [ field ] = [ ] ; } arr [ field ] . push ( value ) ; } return Object . keys ( arr ) . map ( key => ( { key , value : arr [ key ] } ) ) ; } } @ NgModule ( { declarations : [ GroupByPipe ] , exports : [ GroupByPipe ] , } ) export class NgGroupByPipeModule { }