zoukankan      html  css  js  c++  java
  • [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.
  • 相关阅读:
    centos7 安装mysql
    基于flask+requests 个人博客
    python csv、json、pickle数据持久化
    Python之容器、迭代器、生成器
    AJAX常用方法详解
    Python之format详解
    Flask使用MySql数据库
    git 公共服务器
    pci 记录
    检查ept
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6296656.html
Copyright © 2011-2022 走看看