diff options
Diffstat (limited to 'catalog-ui/src/app/ng2/pipes/groupBy.pipe.ts')
-rw-r--r-- | catalog-ui/src/app/ng2/pipes/groupBy.pipe.ts | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/catalog-ui/src/app/ng2/pipes/groupBy.pipe.ts b/catalog-ui/src/app/ng2/pipes/groupBy.pipe.ts new file mode 100644 index 0000000000..17ccc0ca75 --- /dev/null +++ b/catalog-ui/src/app/ng2/pipes/groupBy.pipe.ts @@ -0,0 +1,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] }; }); + } +} |