angular rxjs groupby mergemap return 单值

angular rxjs groupby mergemap return single value

我有以下代码,可以按类型对项目进行分组和计数。我想进行更改并制作 groupBy return 单个对象。

例如,在 mergeMap 完成其操作后,我想附加另一个运算符,它将分析以下结果和 return 单个值 'true' 或 'false' 整个结果集。

{ 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 }

我猜你可以使用 reduce() 方法 (https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/reduce.md)

"Applies an accumulator function over an observable sequence, returning the result of the aggregation as a single element in the result sequence"