首页 > 移动平台 > 详细

[RxJS] Use groupBy in real RxJS applications

时间:2017-01-18 14:48:44      阅读:304      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/Answer1215/p/6296656.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!