Reputation: 691
I get some data from an API and want to fetch it using rxjs's .pipe. What I want to do is to apply an filter if some variable is true.
Here's my working Code: (The UserService returns the API response)
TS
this.filteredOptions = this.userService
.getUserSuggestions(this.displayName, 30)
.pipe(
map(response => response.filter(option => option.eMail != null)),
debounceTime(400),
distinctUntilChanged()
);
Want I want now is to only apply the filter if some variable I set above is true. I tried some stuff with iif() but on my current understanding iif() can't be used to apply different operators.
Greetings!
Solved!
map(response => this.someVariable ? response : response.filter(option => option.eMail != null)),
Use the variable within the map operator. Thanks @Flix
Upvotes: 0
Views: 452
Reputation: 38
Just Use the filter
Operator:
https://www.learnrxjs.io/learn-rxjs/operators/filtering/filter.
.pipe(filter(conditionFn))
Upvotes: 1
Reputation: 709
You can just add the variable condition in the map, like this:
this.filteredOptions = this.userService
.getUserSuggestions(this.displayName, 30)
.pipe(
map(response => someVariable ? response.filter(option => option.eMail != null) : response),
debounceTime(400),
distinctUntilChanged()
);
Upvotes: 1
Reputation: 17494
Use filter
as below:
this.filteredOptions = this.userService
.getUserSuggestions(this.displayName, 30)
.pipe(
filter(response => response.filter(option => option.eMail != null)),
debounceTime(400),
distinctUntilChanged()
);
Upvotes: 0