【问题标题】:How can I use switch instead of multiple if statements?如何使用 switch 而不是多个 if 语句?
【发布时间】:2018-04-23 10:02:11
【问题描述】:

我想知道是否可以将多个 if 语句重写为 switch

问题是switch 运行:

  1. 案例通过检查后的所有代码。这就是为什么 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. 如果一个开关在每种情况下都有中断,它会运行一个通过测试的情况。

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

所以问题是:如何将多个if 语句重写为单个switch 语句?

如果我不能:有没有比多个if 语句更优雅的语法,这表明我在进行类似的比较?

【问题讨论】:

  • 第一个代码示例似乎是您所追求的,您的问题是什么?
  • @George Bailey 不,我不能。检查第一个示例。在 case 传递开关中的值后,它将运行另一个 switch-es 中的所有后续代码,无论这些开关是否与案例具有相同的值。
  • 是的,switch 就是这样工作的。所以第二个应该是你想要的。如果你不喜欢没有breakswitch 行为,那你为什么要切换到switch 呢?
  • 嗯,switch 的方式原本是打算的,它只应该执行一个分支。即使您设法将其破解为您想要的方式(我不确定这是否可能),它也可能因此而误导外部读者。

标签: javascript if-statement switch-statement


【解决方案1】:

如何将多个 if 语句重写为单个 switch 语句?

如果你想匹配多个案例,你不能,合理地。 switch 可以替换if/else,但不能替换一系列独立的ifs,可以匹配多个。

有没有比多个 if 语句更优雅的语法,这表明我在进行类似的比较?

这里的答案往往特定于您正在编写的代码。几个选项供您选择:

参数化成函数

只要你有代码,你一遍又一遍地做同样的事情,参数化它并把它放在一个函数中,然后用参数重复调用函数。

function doTheThing(value) {
  if (arr.includes(value)) {
    console.log('xyz ' + value);
  }
}

例如,在您的示例中:

function doTheThing(value) {
  if (arr.includes(value)) {
    console.log('xyz ' + value);
  }
}

let arr = [1, 3];
doTheThing(1);
doTheThing(2);
doTheThing(3);

let arr = [1, 3];
[1, 2, 3].forEach(value => {
    if (arr.includes(value)) {
        console.log("xyz " + value);
    }
});

或结合这些:

function doTheThing(value) {
  if (arr.includes(value)) {
    console.log('xyz ' + value);
  }
}

let arr = [1, 3];
[1, 2, 3].forEach(doTheThing);

将动作作为函数的查找表

如果你在做不同的事情,一种常见的做法是有一个价值到行动的查找表,例如:

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

1() { } 表示法可能看起来很奇怪,因为您不会经常看到带有数字名称的属性的方法表示法,但它完全有效。在不支持方法符号的旧环境中:

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

旁注:你永远不需要=== trueArray#includes。它总是返回一个布尔值。

【讨论】:

  • 这个答案有点骗人。它适用于这个例子。但是拥有多个 if 语句的目的是根据输入值执行不同的操作/功能。因为您假设我想使用不同的输入值/变量运行相同的代码,所以您的代码可以工作。但是如果我想运行不同的函数,我仍然需要相同的 if 语句,我想在 doTheThing-function 中简化这些语句!话虽如此,你确实指出了这一点。但我只是想强调,这种方法有一个特定的用例。
  • @RMo:如果各个案例的逻辑明显不同,请参阅更新后的答案。
  • 这个答案中的见解绝对值得一看,以了解同一件事的一些替代方法。特别是查找表是一种非常有趣的方法。 (尽管如此,我会坚持使用多个 if 语句,因为我觉得它更容易阅读)。
猜你喜欢
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
  • 1970-01-01
  • 2010-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多