【问题标题】:Are there any way to filter of{} not put aditional map?有什么方法可以过滤{}不放额外的地图吗?
【发布时间】:2021-01-04 02:49:48
【问题描述】:

我有管道,当发生一些错误时它会返回of{}。所以我需要将它过滤到其他代码流中。目前我使用了以下 senario。我需要知道,有什么方法可以删除地图并在现有过滤器中过滤它?

 readonly k$ = combineLatest(
    [this.x$.pipe(filter(isNotNullOrUndefined), //this filter not filtering  of{} type.
      map((res: A) => {  //need to remove this map
        return res;
      })
    ), this.y$]).pipe(shareReplayUntil(this.destroySub));

【问题讨论】:

    标签: typescript rxjs ngrx rxjs-pipeable-operators


    【解决方案1】:

    您可以返回of(null) 而不是of({}),以便您的过滤函数isNotNullOrUndefined 正确过滤null 值。

    【讨论】:

      【解决方案2】:

      你的意思是一个空对象of({})的可观察对象吗?

      你可以用Object.keys(res).length !== 0检查它是否为空

      filter((res: A) => isNotNullOrUndefined(res) && Object.keys(res).length !== 0)

      【讨论】:

        猜你喜欢
        • 2015-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-16
        • 1970-01-01
        • 1970-01-01
        • 2019-10-22
        • 2020-08-04
        相关资源
        最近更新 更多