I am making a service call having 3 parameters based on IF condition else setting 5 parameters. Need to use Ternary operator to set 4th parameter. Else do not set anything. Below is the code
if (allow) {
return forkJoin(param1, param2, param3) // Here i need to set param4 based on other condition
} else {
return forkJoin(param1, param2, param3, param4, param5)
}
I am trying to set using below
if (allow) {
return forkJoin(param1, param2, this.vipPerson ? param4 : null, param3)
// But using ternary operator i cant send NULL if this.vipPerson is False. What to do so that
// if vipPerson is TRUE it should be like this
return forkJoin(param1, param2, param3, param4)
// Else it should be like this
return forkJoin(param1, param2, param3)
}
I cant send NULL. Finding the way to send nothing in case if its False
forkJoin takes array of observables, null is not observable. change it to:-
of(null)
you need to import of from rxjs.
A more declarative way would be to define the two possible observables, then use the iif()
operator to run the condition. While it's not exactly ternary, it's as close as any RxJS operator gets.
const basic$ = forkJoin(param1, param2, param3);
const vip$ = forkJoin(param1, param2, param3, param4);
return iif(()=>this.vipPerson, vip$, basic$)
https://www.learnrxjs.io/learn-rxjs/operators/conditional/iif
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.