Nathan Elg
Nathan Elg

Reputation: 187

Why do the RxJS filter method not work for me

Having issues using RxJS operators the way they were intended to be used.

My endpoint returns Observable<Array<TaskReprintReasonCode>>

I then use the async pipe to subscribe to this observable.

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

This works great until i need to filter or map something out of that list of reasons.

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

Error Produced from my IDE

I am guessing it has to do with the way I am defining the type coming back from the DB. Is it bad practice to be naming Observabe<INSERTTYPE[]>

Upvotes: 0

Views: 4051

Answers (1)

Reactgular
Reactgular

Reputation: 54821

Observable<Array<TaskReprintReasonCode>>

Defines a collection of TaskReprintReasonCode objects.

You can use the map() operator to modify the value of an item in a stream. If you want to reduce the array to only those that have a property of "new", then you would use the filter() from the array prototype.

    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

Upvotes: 2

Related Questions