zl程序教程

您现在的位置是:首页 >  其它

当前栏目

[RxJS] Split an RxJS Observable into groups with groupBy

with an Rxjs INTO split Observable GroupBy groups
2023-09-14 09:00:52 时间

groupBy() is another RxJS operator to create higher order observables. In this lesson we will learn how groupBy works for routing source values into different groups according to a calculated key.

 

const numbersObservable = Rx.Observable.interval(500).take(5);

numbersObservable
  .groupBy(x => x % 2)
  .map(innerObs => innerObs.count())
  .mergeAll()
  .subscribe(x => console.log(x));

/*
--0--1--2--3--4|

 groupBy(x => x % 2)
 
--+--+---------|
  \  \
  \  1-----3---|
  0-----2-----4|
  
 map(innerObs => innerObs.count())
 
--+--+---------|
  \  \
  \  ---------2|
  ------------3|
  
 mergeAll
 
--------------(3,2)|

*/