[英]How can I use switch instead of multiple if statements?
I was wondering if it is possible to rewrite multiple if
statements into a switch
. 我想知道是否可以将多个if
语句重写到switch
。
The problem is that a switch
runs: 问题是switch
在运行:
all code after a case passes a check. 案件通过检查后的所有代码。 Which is why the case statement runs all code after the first case. 这就是case语句在第一个case之后运行所有代码的原因。
let arr = [1, 3]; if( arr.includes(1) === true ) { console.log('if 1'); } if( arr.includes(2) === true) { console.log('if 2'); } if( arr.includes(3) === true) { console.log('if 3'); } switch( true ){ case arr.includes(1): console.log('switch 1'); case arr.includes(2): console.log('switch 2'); case arr.includes(3): console.log('switch 3'); }
let arr = [1, 3]; if( arr.includes(1) === true ) { console.log('if 1'); } if( arr.includes(2) === true) { console.log('if 2'); } if( arr.includes(3) === true) { console.log('if 3'); } switch( true ){ case arr.includes(1): console.log('switch 1'); break; case arr.includes(2): console.log('switch 2'); break; case arr.includes(3): console.log('switch 3'); break; }
So the question is: How can I rewrite multiple if
statements into a single switch
statement? 所以问题是: 如何将多个if
语句重写为单个switch
语句?
If I can't: Is there another more elegant syntax than the multiple if
statements, that makes it apparent that I am making similar comparisons? 如果不能: 是否有比if
语句更优雅的语法,这显然使我进行了类似的比较?
How can I rewrite multiple if statements into a single switch statement? 如何将多个if语句重写为单个switch语句?
You can't, reasonably, if you want multiple cases to match. 如果要匹配多个案例,则无法合理地进行。 switch
can replace if
/ else
, but not a series of independent if
s where more than one can match. switch
可以替换if
/ else
,但是不能替换一系列独立的if
,其中多个可以匹配。
Is there another more elegant syntax than the multiple if statements, that makes it apparent that I am making similar comparisons? 除了多重if语句之外,还有没有更优雅的语法,这显然使我进行了类似的比较?
The answer here will tend to be specific to the code you're writing. 这里的答案往往是特定于您正在编写的代码。 A couple of options for you: 几个选择为您:
Whenever you have code where you're doing the same thing over and over again, parameterize it and put it in a function, then call the function repeatedly with the parameters. 只要您有重复执行相同操作的代码,就对其进行参数化并将其放入函数中,然后使用参数重复调用该函数。
function doTheThing(value) {
if (arr.includes(value)) {
console.log('xyz ' + value);
}
}
For instance, in your example: 例如,在您的示例中:
function doTheThing(value) { if (arr.includes(value)) { console.log('xyz ' + value); } } let arr = [1, 3]; doTheThing(1); doTheThing(2); doTheThing(3);
or 要么
let arr = [1, 3]; [1, 2, 3].forEach(value => { if (arr.includes(value)) { console.log("xyz " + value); } });
or combining those: 或结合这些:
function doTheThing(value) { if (arr.includes(value)) { console.log('xyz ' + value); } } let arr = [1, 3]; [1, 2, 3].forEach(doTheThing);
If you're doing different things, one common practice is to have a lookup table of value-to-action, eg: 如果您做不同的事情,一种常见的做法是拥有一个价值对行动的查询表,例如:
const actionsByValue = { 1() { console.log("This is the thing for #1"); }, 2() { console.log("This is something else for #2"); }, 3() { console.log("Different logic again for #3"); } }; const nop = () => { }; let arr = [1, 3]; arr.forEach(value => { (actionsByValue[value] || nop)(value); });
That 1() { }
notation may look odd because you don't see method notation with properties with numeric names very often, but it's perfectly valid. 1() { }
表示法可能看起来很奇怪,因为您不会经常看到带有带有数字名称的属性的方法表示法,但这是完全有效的。 In old environments that don't support method notation: 在不支持方法表示法的旧环境中:
const actionsByValue = {
1: function() {
console.log("This is the thing for #1");
},
2: function() {
console.log("This is something else for #2");
},
3: function() {
console.log("Different logic again for #3");
}
};
Side note: You never need === true
with Array#includes
. 旁注: Array#includes
不需要=== true
。 It always returns a boolean. 它总是返回一个布尔值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.