码迷,mamicode.com
首页 > 移动开发 > 详细

[RxJS] Use groupBy in real RxJS applications

时间:2017-01-18 14:51:02      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:console   delay   concat   group   scribe   code   sso   can   for   

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

标签:console   delay   concat   group   scribe   code   sso   can   for   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!