[RxJS] Use groupBy in real RxJS applications

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[RxJS] Use groupBy in real RxJS applications相关的知识,希望对你有一定的参考价值。

This lesson will show when to apply groupBy in the real world. This RxJS operator is best suited when a source observable represents many data sources, e.g. an observable for multitouch events.

 

const busObservable = Rx.Observable.of(
  {code: en-us, value: -TEST-},
  {code: en-us, value: hello},
  {code: es, value: -TEST-},
  {code: en-us, value: amazing},
  {code: pt-br, value: -TEST-},
  {code: pt-br, value: olá},
  {code: es, value: hola},
  {code: es, value: mundo},
  {code: en-us, value: world},
  {code: pt-br, value: mundo},
  {code: es, value: asombroso},
  {code: pt-br, value: maravilhoso}
).concatMap(x => Rx.Observable.of(x).delay(500));

const all = busObservable
  .groupBy(obj => obj.code) // 2-d obs
  .mergeMap(innerObs => innerObs.skip(1).map(obj => obj.value));

all.subscribe(x => console.log(x));
/*
"hello"
"amazing"
"olá"
"hola"
"mundo"
"world"
"mundo"
"asombroso"
"maravilhoso"
*/

 

  • The ‘groupBy‘ return a 2-d observable, can use ‘switchMap‘ or ‘mergeMap‘ to conver to 1-d observable.

 

以上是关于[RxJS] Use groupBy in real RxJS applications的主要内容,如果未能解决你的问题,请参考以下文章

[RxJS] Learn How To Use RxJS 5.5 Beta 2

[RxJS] Use takeUntil instead of manually unsubscribing from Observables

[Angular2 Form] Use RxJS Streams with Angular 2 Forms

[RxJS] Error Handling in RxJS

[RxJS] Flatten a higher order observable with mergeAll in RxJS

[RxJS] Flatten a higher order observable with concatAll in RxJS