代码之家  ›  专栏  ›  技术社区  ›  danday74

rxjs如何最好地将args数组转换为对象

  •  0
  • danday74  · 技术社区  · 7 年前

    withLatestFrom :

    .pipe(
      withLatestFrom(observable1$),
      map(([one, two]) => ({one, two})),
      tap((args) => { console.log(args) }}
    )
    

    forkJoin :

    .pipe(
      mergeMap((action) => forkJoin(
        of(action),
        observable1$.take(1),
        observable2$.take(1)
      )),
      map(([one, two, three]) => ({one, two, three})),
      tap((args) => { console.log(args) }} 
    )
    

    这两种方法都将管道数组转换为对象(我觉得更容易管理)。

    我可以映射到mergeMap中的对象吗?有没有另一个运算符来简化代码?欢迎回答任何一个(或多个)问题。谢谢

    1 回复  |  直到 7 年前
        1
  •  3
  •   cartant    7 年前

    一些RxJS操作符和函数目前支持结果选择器。例如,可以在 forkJoin 打电话,像这样:

    .pipe(
      mergeMap((action) => forkJoin(
        of(action),
        observable1$.take(1),
        observable2$.take(1),
        (one, two, three) => ({ one, two, three })
      )),
      tap((args) => { console.log(args) }}
    )
    

    result selectors are now deprecated -因为删除它们将大大简化实现和API—以及 recommended approach 是你拿走的那个:用 map 接线员。