groupBy
签名: groupBy(keySelector: Function, elementSelector: Function): Observable
基于提供的值分组成多个 observables
示例
示例 1: 根据属性分组
( StackBlitz |
jsBin |
jsFiddle )
import { from } from 'rxjs/observable/from';
import { groupBy, mergeMap, toArray } from 'rxjs/operators';
const people = [
{ name: 'Sue', age: 25 },
{ name: 'Joe', age: 30 },
{ name: 'Frank', age: 25 },
{ name: 'Sarah', age: 35 }
];
// 发出每个 people
const source = from(people);
// 根据 age 分组
const example = source.pipe(
groupBy(person => person.age),
// 为每个分组返回一个数组
mergeMap(group => group.pipe(toArray()))
);
/*
输出:
[{age: 25, name: "Sue"},{age: 25, name: "Frank"}]
[{age: 30, name: "Joe"}]
[{age: 35, name: "Sarah"}]
*/
const subscribe = example.subscribe(val => console.log(val));
其他资源
- groupBy - 官方文档
- 使用 RxJS 的 groupBy 操作符分组成高阶 observables - André Staltz
- 在真实的 RxJS 应用中使用 groupBy - André Staltz
源码: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/groupBy.ts