Skip to content

Commit

Permalink
docs(operators): examples for groupBy operator (#2219)
Browse files Browse the repository at this point in the history
* docs(operators): examples for groupBy operator

* style(lint): fix trailing whitespace
  • Loading branch information
feloy authored and benlesh committed Apr 3, 2017
1 parent 840def0 commit c60c387
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/operator/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,52 @@ export function groupBy<T, K, R>(this: Observable<T>, keySelector: (value: T) =>
*
* <img src="./img/groupBy.png" width="100%">
*
* @example <caption>Group objects by id and return as array</caption>
* Observable.of<Obj>({id: 1, name: 'aze1'},
* {id: 2, name: 'sf2'},
* {id: 2, name: 'dg2'},
* {id: 1, name: 'erg1'},
* {id: 1, name: 'df1'},
* {id: 2, name: 'sfqfb2'},
* {id: 3, name: 'qfs3'},
* {id: 2, name: 'qsgqsfg2'}
* )
* .groupBy(p => p.id)
* .flatMap( (group$) => group$.reduce((acc, cur) => [...acc, cur], []))
* .subscribe(p => console.log(p));
*
* // displays:
* // [ { id: 1, name: 'aze1' },
* // { id: 1, name: 'erg1' },
* // { id: 1, name: 'df1' } ]
* //
* // [ { id: 2, name: 'sf2' },
* // { id: 2, name: 'dg2' },
* // { id: 2, name: 'sfqfb2' },
* // { id: 2, name: 'qsgqsfg2' } ]
* //
* // [ { id: 3, name: 'qfs3' } ]
*
* @example <caption>Pivot data on the id field</caption>
* Observable.of<Obj>({id: 1, name: 'aze1'},
* {id: 2, name: 'sf2'},
* {id: 2, name: 'dg2'},
* {id: 1, name: 'erg1'},
* {id: 1, name: 'df1'},
* {id: 2, name: 'sfqfb2'},
* {id: 3, name: 'qfs1'},
* {id: 2, name: 'qsgqsfg2'}
* )
* .groupBy(p => p.id, p => p.anme)
* .flatMap( (group$) => group$.reduce((acc, cur) => [...acc, cur], ["" + group$.key]))
* .map(arr => ({'id': parseInt(arr[0]), 'values': arr.slice(1)}))
* .subscribe(p => console.log(p));
*
* // displays:
* // { id: 1, values: [ 'aze1', 'erg1', 'df1' ] }
* // { id: 2, values: [ 'sf2', 'dg2', 'sfqfb2', 'qsgqsfg2' ] }
* // { id: 3, values: [ 'qfs1' ] }
*
* @param {function(value: T): K} keySelector A function that extracts the key
* for each item.
* @param {function(value: T): R} [elementSelector] A function that extracts the
Expand Down

0 comments on commit c60c387

Please sign in to comment.