[英]Is it possible to put a switch statement inside of a Conditional (ternary)?
I'm really new to using Conditional (ternary) operator. 我真的很喜欢使用Conditional(三元)运算符。 Is it possible to put a switch statement in this operator?
是否可以在此运算符中放置switch语句?
This is what i tried: 这是我试过的:
function find(cn, romanNum) {
if (cn >= 1 && cn <= 3) {
return repeatString(romanNum[2], cn);
}
cn > 5 && cn < 9 ? return romanNum[1] + repeatString(romanNum[2], cn - 5) : switch(cn) {
case 4:
return romanNum[2] + romanNum[1];
case 5:
return romanNum[1];
case 9:
return romanNum[2] + romanNum[0];
}
}
I'm doing something wrong i know, but what is it? 我知道我做错了什么,但它是什么? Everyone is new to something at some point in time.
每个人在某个时间点都是新事物。
Error: 错误:
'return'
. 'return'
。 ':'
and instead saw 'romanNum'
. ':'
而是看到'romanNum'
。 '}'
to match '{'
from line 58 and instead saw ':'
'}'
匹配来自第58行的'{'
而是看到':'
This is the right es5 code: 这是正确的es5代码:
function find(cn, romanNum) {
if (cn >= 1 && cn <= 3) {
return repeatString(romanNum[2], cn);
} else if (cn == 4) {
return romanNum[2] + romanNum[1];
} else if (cn == 5) {
return romanNum[1];
} else if (cn == 9) {
return romanNum[2] + romanNum[0];
}
if (cn > 5 && cn < 9) {
return romanNum[1] + repeatString(romanNum[2], cn - 5);
}
}
Help? 救命?
tl;dr: kind of, but you shouldn't do it since it is not that readable. tl; dr:有点,但你不应该这样做,因为它不可读。
Always keep code short an simple but verbose. 始终保持代码短,简单但冗长。
Long answer: You could wrap your switch inside an immediately invoked anonymous function. 答案很长:你可以将你的开关包装在一个立即调用的匿名函数中。
const a = 20; const condition = a > 100; const result = condition ? true : ( () => { switch ( a ) { case 11: return 22; case 20: return 21; default: return 100; } } )(); console.log( result );
But not only is this more complex but also harder to read. 但这不仅更复杂,而且更难阅读。
It is better to use a verbose coding style. 最好使用冗长的编码风格。 In your case something like this would be cleaner and more readable:
在你的情况下,这样的东西会更干净,更具可读性:
function test( a ) { const condition = a > 100; if ( condition ) { return true; } switch ( a ) { case 20: return 21; default: return 100; } } console.log( test( 20 ) );
You can use an IIFE: 您可以使用IIFE:
return condition
? (() => {
switch (val) {
case x: return a;
case y: return b;
default: return c;
}
})()
: other;
however this is horribly unreadable and not even shorter than the simple if
statement. 然而,这是非常难以理解的,甚至不比简单的
if
语句短。
That said, there is a much better choice than switch
for looking up values: a lookup table! 这就是说,有一个更好的选择,而不是
switch
用于查找值:查找表! Just use an object, array or Map
and use your cn
as the property name. 只需使用对象,数组或
Map
并使用您的cn
作为属性名称。
const table = {
1: repeatString(romanNum[2], 1),
2: repeatString(romanNum[2], 2),
3: repeatString(romanNum[2], 3),
4: romanNum[2] + romanNum[1],
5: romanNum[1],
6: romanNum[1] + repeatString(romanNum[2], 1),
7: romanNum[1] + repeatString(romanNum[2], 2),
8: romanNum[1] + repeatString(romanNum[2], 3),
9: romanNum[2] + romanNum[0]
};
function find(cn) {
return table[cn];
}
// using an array similar to the object above:
table = [
...Array.from({length: 4}, (_, i) => repeatString(romanNum[2], i))
romanNum[2] + romanNum[1],
...Array.from({length: 4}, (_, i) => romanNum[1] + repeatString(romanNum[2], i)),
romanNum[2] + romanNum[0]
];
You could use a nested ternary whith an object as replacement for the select
structure. 您可以使用嵌套的三元组作为
select
结构的替代对象。
function find(cn, romanNum) {
return cn >= 1 && cn <= 3
? repeatString(romanNum[2], cn)
: cn > 5 && cn < 9
? romanNum[1] + repeatString(romanNum[2], cn - 5)
: {
4: romanNum[2] + romanNum[1],
5: romanNum[1],
9: romanNum[2] + romanNum[0]
}[cn];
}
A step further, you could omit the second ternary and use the first condition as default value for the result of non existent property of the object. 更进一步,您可以省略第二个三元组,并使用第一个条件作为对象的不存在属性的结果的默认值。
function find(cn, romanNum) {
return cn >= 1 && cn <= 3
? repeatString(romanNum[2], cn)
: {
4: romanNum[2] + romanNum[1],
5: romanNum[1],
9: romanNum[2] + romanNum[0]
}[cn] || romanNum[1] + repeatString(romanNum[2], cn - 5);
}
U cant use the return like this just a small example to show how to return in normal ternary operator 你不能使用这样的返回只是一个小例子来说明如何在普通的三元运算符中返回
function find(cn, romanNum) { if (cn >= 1 && cn <= 3) { return 2; } return (cn > 5 && cn < 9) ? ( romanNum[1] + 2, cn - 5) : 3
} }
No its not possible to use switch statement in ternary operator because switch is a statement not a condition or expression 不能在三元运算符中使用switch语句,因为switch是一个语句而不是条件或表达式
Better use if -else 如果-else更好用
If you want to stick to ternary then make the changes like 如果你想坚持三元,那么就做出改变
function find(cn, romanNum) { if (cn >= 1 && cn <= 3) { return repeatString(romanNum[2], cn); } return cn > 5 && cn < 9 ? romanNum[1] + repeatString(romanNum[2], cn - 5) : (cn===4 ? romanNum[2] + romanNum[1]: (cn==5 ? romanNum[1] : romanNum[2] + romanNum[0])) } console.log(find(2,[1,2,3,4]))
Though I wont prefer nested ternary operator as it hinders the readability of the code . 虽然我不喜欢嵌套的三元运算符,因为它阻碍了代码的可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.