具有多个条件的 JS 嵌套三元

Des*_*xia 0 javascript conditional-operator reactjs

本质上我有以下问题:

if condition A & B are true ->. do thing A
if only condition A is true -> do thing B 
else -> do thing C
Run Code Online (Sandbox Code Playgroud)

我试过这个:

const myThing = conditionA ? conditionB ? thingA :
conditionA ? thingB : thingC;
Run Code Online (Sandbox Code Playgroud)

它不喜欢这种语法,但我不确定它有什么问题。

xde*_*akv 5

    const myThing = (conditionA && conditionB) ? thingA : (conditionA) ? thingB : thingC;
       
Run Code Online (Sandbox Code Playgroud)

与以下相同:

if(conditionA && conditionB){
   thingA
}
else if(conditionA){
   thingB
} else {
   thingC
}
Run Code Online (Sandbox Code Playgroud)