代码之家  ›  专栏  ›  技术社区  ›  Rob Monhemius

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

  •  0
  • Rob Monhemius  · 技术社区  · 8 年前

    我想知道是否可以重写多个 if 语句转换为 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;
    }

    所以问题是: 如何重写多个 如果 将语句转换为单个 转换 陈述

    如果我不能: 还有比multiple更优雅的语法吗 如果 声明,这表明我正在进行类似的比较?

    1 回复  |  直到 8 年前
        1
  •  5
  •   T.J. Crowder    8 年前

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

    如果您希望多个案例匹配,那么您就无法合理地进行匹配。 switch 可以替换 if / else ,但不是一系列独立的 如果 这是一个多人可以匹配的地方。

    有没有比多重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");
      }
    };
    

    旁注:你永远不需要 === true 具有 Array#includes 。它始终返回布尔值。