比较并分配数字

cha*_*ian 3 javascript compare numbers libraries underscore.js

这个程序可能已经在某种形式的SO上涵盖,但我不知道如何找到它.

我想将一个值与另外两个值进行比较,并在同一步骤中理想地分配变量.

就我而言,我有一个用户Id(A)和两个示例用户ID(B和C)

在这种情况下,B或C必须等于A.

我想检查A == B或A == C然后将变量(其他)设置为不等于A的B或C id.

我知道我能做到

 if (A == B) {
   var other = C;
   return other
 } 
 else {
   var other = B
   return other
 }
Run Code Online (Sandbox Code Playgroud)

很简单,但有谁知道更有效的方式?任何JS库都是允许的.

Jam*_*lly 5

你可以用三元条件做到这一点:

var other = A == B ? C : B;
Run Code Online (Sandbox Code Playgroud)

在这种情况下,如果AB相同,other将被设置为C,否则它将被设置为B.