为什么RXJS过滤器方法对我不起作用

时间:2019-04-17 15:01:34

标签: angular rxjs observable rxjs5

使用RXJS运算符的方式存在问题。

我的端点返回Observable<Array<TaskReprintReasonCode>>

然后我使用异步管道订阅此可观察对象。

this.reason$ = this.taskService.getTaskReprintReasonCodes();

这很有效,直到我需要从原因列表中过滤或映射某些内容为止。

this.reasons$ = this.taskService.getTaskReprintReasonCodes().pipe(filter(r => r.reasonDescription === 'New'))

Error Produced from my IDE

我猜想这与我定义从数据库返回的类型的方式有关。命名Observabe<INSERTTYPE[]>

是不好的做法

1 个答案:

答案 0 :(得分:0)

Observable<Array<TaskReprintReasonCode>>

定义TaskReprintReasonCode个对象的集合。

您可以使用map()运算符来修改流中项目的值。如果只想将数组简化为属性为"new"的数组,则可以使用数组原型中的filter()

    this.taskService.getTaskReprintReasonCodes().pipe(
        map((arr:TaskReprintReasonCode[]) => {
             return arr.filter(r => r.reasonDescription === 'New');
        )
    )

Array.filter()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Rxjs.map()

https://www.learnrxjs.io/operators/transformation/map.html