import { Pipe , PipeTransform } from '@angular/core' ; /**
 * <ul>
 *   <li *ngFor="let object of myArray | groupBy:'color'"></li>
 * </ul>
 */ @ Pipe ( { name : 'groupBy' } ) export class GroupByPipe < T > implements PipeTransform { transform ( collection , property ) { if ( ! collection ) { return null ; } const groupedCollection = collection . reduce ( ( previous , current ) => { if ( ! previous [ current [ property ] ] ) { previous [ current [ property ] ] = [ current ] ; } else { previous [ current [ property ] ] . push ( current ) ; } return previous ; } , { } ) ; return Object . keys ( groupedCollection ) . map ( ( key ) => ( { key , value : groupedCollection [ key ] , } ) ) ; } }