【发布时间】:2018-03-09 05:13:28
【问题描述】:
我有以下代码按类型对项目进行分组和计数。我想进行更改并制作 groupBy return 单个对象。
例如,在 mergeMap 完成其操作后,我想附加另一个运算符,它将分析以下结果并为整个结果集返回单个值“真”或“假”。
{ type: 'foo', total: 1 }
{ type: 'bar', total: 2 }
const list = [{ type: 'foo' }, { type: 'bar' }, { type: 'bar' }];
Observable.from( list ).groupBy( x => x.type )
.mergeMap( list$ => { // each emission is a stream
/* A stream of "aggregated" data. */
const count$ = list$.count();
/* Format the result. */
return count$.map( count => ({ type: list$.key, count }));
}).subscribe(r => console.log(r));
这会发出:
{ type: 'foo', total: 1 }
{ type: 'bar', total: 2 }
【问题讨论】: