简体   繁体   English

如何使用switch而不是多个if语句?

[英]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在运行:

  1. 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'); } 

    1. if a switch has breaks in every case, it runs a single case, that passes the test. 如果交换机在每种情况下都发生故障,则它将运行一个案例,并通过测试。

 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: 几个选择为您:

Parameterize into a function 参数化为函数

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); 

Lookup table with actions as functions 具有动作作为功能的查找表

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.

相关问题 我怎样才能使用条件语句来切换我标题的颜色? - how can i use conditional statements to switch colors of my heading? 我可以把它变成 1 个 switch case 而不是许多 if else 语句吗 - can i make this into 1 switch case instead of many if else statements 如何使用Enter键而不是通过开关单击按钮 - How can I use Enter Key instead of clicking the button by switch 我在哪里可以使用 JS Switch case 语句来转换这些多个 If/Else 语句? - Where can I use the JS Switch case statement to convert these multiple If/Else statements? 何时可以让switch语句使用多个case语句的人能举一个例子吗? - Can someone give an example when to let switch statements use multiple case statements? 如何在子开关语句中使用函数参数? - How do I use function arguments in child switch statements? JavaScript如何使用数组和循环而不是多个if语句 - JavaScript how to use array and loop instead of multiple if statements Javascript:如何为基于表达式的条件使用对象文字而不是 if 和 switch 语句? - Javascript: How to use object literals instead of if and switch statements for expression-based conditions? 我可以将.show()或.hide()与switch 1/0而不是If语句一起使用 - Can I use .show() or .hide() with a switch 1/0 instead of an If statement 我可以将IF语句放入切换用例函数吗? - Can I put IF statements in a switch case function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM