【问题标题】:convert rxjs5 operators into rxjs6在 rxjs 6 中转换 rxjs 运算符
【发布时间】:2019-11-07 18:33:53
【问题描述】:

我有以下用 rxjs5 编写的代码,但它与 rxjs6 中断了

有人可以帮我写 rxjs 6

它失败的mergemap接收groupedObserable,它没有count方法和filter方法也不存在。

list [

{id: '1', type: 't1',  name: 'f1', des:'d1', selected: true},
{id: '2', type: 't1',  name: 'f2', des:'d2', selected: false},

{id: '3', type: 't1',  name: 'f11', des:'d11', selected: false},
{id: '4', type: 't1',  name: 'f22', des:'d22', selected: true},
]

Observable.from(list)
.filter(a => a.name != null)
.groupBy(i => i.type)
.mergeMap(list => {
let count = list.count;
let selectedCount = 0;
list.filter( f => f.selected).count.subscribe(c => selectedCount = c)
return count.map(count =>  {
   {
     key: list.key,
     totalCount: count,
     selected: selectedCount
   }
}
}).reduce((x, y) => {
x.isValid = x.selectedCount > 0 
return x;
}).subscribe(r => {
  console.log(r + 'any item selected')
}
)

当我尝试在 rxjs6 中编写时,我只能取得进展,直到这里 提前致谢。

from(list)
    .pipe(
    filter( s=> s.name != null) ,
    groupBy(i => i.type),
    mergeMap( (value, index) => {
      value.count // that's where it starts to fail
    }
    ))

【问题讨论】:

    标签: rxjs rxjs5 rxjs6


    【解决方案1】:

    等效的rxjs6代码应该是这样的:

    from(list)
          .pipe(
            filter(a => a.name != null),
            groupBy(i => i.type),
            mergeMap((p) => {
              return p.pipe(
                        filter(f => f.selected),
                        count(),
                        mergeMap(c => {
                          return p.pipe(
                            count(),
                            map(totalCount => {
                              return {
                                key: p.key,
                                totalCount: totalCount,
                                selected: c
                              };
                            })
                          );
                        })
                    );
            }),
            reduce((x, y) => {
              //please adjust your code here as i could not see isValid on x
              x.isValid = x.selectedCount > 0; 
              return x;
             })
          ).subscribe(r => {
            console.log(r + 'any item selected')
          }
          )
    

    希望它能提供一个关于如何进行的想法。

    【讨论】:

    • 一些如何执行无法到达 p.pipe( map(totalCount => { return { key: p.key, totalCount: totalCount, selected: c }; })
    • 你能帮我了解一下总计数是从哪里来的吗?
    • @d-man 啊!我忘了使用count 运算符。我已经编辑了我的答案。现在你应该从 totalCount 的来源获得。希望对您有所帮助。
    猜你喜欢
    • 2021-01-16
    • 2020-10-16
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多