简体   繁体   English

三元运算符 Javascript 在其他情况下什么都不做

[英]Ternary Operator Javascript in Else case Do Nothing

I am making a service call having 3 parameters based on IF condition else setting 5 parameters.我正在根据 IF 条件进行具有 3 个参数的服务调用,否则设置 5 个参数。 Need to use Ternary operator to set 4th parameter.需要使用三元运算符来设置第 4 个参数。 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我无法发送 NULL。找到不发送任何内容的方法,以防万一它是 False

forkJoin takes array of observables, null is not observable. forkJoin 采用可观察数组,null 不可观察。 change it to:-将其更改为:-

of(null)

you need to import of from rxjs.您需要从 rxjs 导入。

A more declarative way would be to define the two possible observables, then use the iif() operator to run the condition.一种更具声明性的方法是定义两个可能的可观察对象,然后使用iif()运算符来运行条件。 While it's not exactly ternary, it's as close as any RxJS operator gets.虽然它不完全是三元的,但它与任何 RxJS 运算符一样接近。

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 https://www.learnrxjs.io/learn-rxjs/operators/conditional/iif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM