[英]Combine two values with ternary operator in Javascript
Having the following code snippet:具有以下代码片段:
const value1 = isSomething ? 'a' : isSomethingElse ? 'b' : 'c';
const value2 = isSomething ? 1 : isSomethingElse ? 2 : 3;
Those two variables are depending of the same other variables behavior ( isSomething
and isSomethingElse
)这两个变量取决于相同的其他变量行为( isSomething
和isSomethingElse
)
Is there a way to write this code into a single line?有没有办法把这段代码写成一行?
You can use arrays, and destructuring assignment, but it will not be faster:您可以使用 arrays 和解构赋值,但不会更快:
const [value1, value2] = isSomething ? ['a', 1]
: isSomethingElse ? ['b', 2] : ['c', 3];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.