summaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pipes/groupBy.pipe.ts
blob: 17ccc0ca755b56011639b95c3d385f7e8342635b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * Created by rc2122 on 5/17/2017.
 */
import {Pipe, PipeTransform} from '@angular/core';

@Pipe({name: 'groupBy'})
export class GroupByPipe implements PipeTransform {
    transform(value: Array<any>, field: string): Array<any> {
        const groupedObj = value.reduce((prev, cur)=> {
            if(!prev[cur[field]]) {
                prev[cur[field]] = [cur];
            } else {
                prev[cur[field]].push(cur);
            }
            return prev;
        }, {});
        return Object.keys(groupedObj).map((key:string) => {return { key, value: groupedObj[key] }; });
    }
}