groupBy
연산자(operator) 정의: groupBy(keySelector: Function, elementSelector: Function): Observable
Group into observables based on provided value.
Examples
Example 1: Group by property
const people = [{name: 'Sue', age:25},{name: 'Joe', age: 30},{name: 'Frank', age: 25}, {name: 'Sarah', age: 35}];
//emit each person
const source = Rx.Observable.from(people);
//group by age
const example = source
.groupBy(person => person.age)
//return as array of each group
.flatMap(group => group.reduce((acc, curr) => [...acc, curr], []))
/*
output:
[{age: 25, name: "Sue"},{age: 25, name: "Frank"}]
[{age: 30, name: "Joe"}]
[{age: 35, name: "Sarah"}]
*/
const subscribe = example.subscribe(val => console.log(val));
Additional Resources
- groupBy - Official docs
- Group higher order observables with RxJS groupBy - André Staltz
- Use groupBy in real RxJS applications - André Staltz
Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/groupBy.ts