简体   繁体   English

开关盒内组合

[英]Combination inside switch case

I used to have a simple switch case , like this:我曾经有一个简单的switch case ,像这样:

 const expr = 'Papayas'; switch (expr) { case 'Oranges': console.log('Oranges'); break; case 'Mangoes': console.log('Mangoes'); break; case 'Papayas': console.log('Papayas'); break; }

but now some criteria changed and I need to add one rule for all three cases and leave the old functionality as it was.但现在一些标准发生了变化,我需要为所有三种情况添加一条规则,并保持旧功能不变。 So in case, I'll have Papayas , my resalt would be All and Papayas .因此,以防万一,我将拥有Papayas ,我的盐将是AllPapayas I tried to combine it like this:我试着像这样组合它:

 const expr = 'Papayas'; switch (expr) { case 'Oranges': case 'Mangoes': case 'Papayas': console.log('All'); case 'Oranges': console.log('Oranges'); break; case 'Mangoes': console.log('Mangoes'); break; case 'Papayas': console.log('Papayas'); break; }

but that doesn't work correctly.但这不能正常工作。 I always get All and Oranges in the result.我总是在结果中得到AllOranges Is there a way to do it with one switch case ?有没有办法用一个switch case做到这一点?

You can make something like this:你可以做这样的事情:

const output = ['All'];

switch (expr) {
  case 'Oranges':
    output.push('Oranges');
    break;
  case 'Mangoes':
    output.push('Mangoes');
    break;
  case 'Papayas':
    output.push('Papayas');
    break;
}

console.log(...output);

What happens if I forgot a break?如果我忘记休息会怎样?

If you forget a break then the script will run from the case where the criterion is met and will run the cases after that regardless if a criterion was met.如果您忘记了中断,则脚本将从满足条件的情况下运行,并且无论是否满足条件都将在此之后运行这些情况。 - the MDN JS docs on switch - switch上的 MDN JS 文档

What happens in your case is that it matches the case 'Papayas': console.log('All') and then runs the next case 'Oranges': console.log('Oranges') break;在您的情况下发生的情况是它与case 'Papayas': console.log('All')匹配,然后运行下一个case 'Oranges': console.log('Oranges') break; despite it not being met;尽管没有得到满足; afterwards comes the break of that case - without this you'd also be outputting Mangoes and Papayas .之后是这种情况的break - 如果没有这个,你也会输出MangoesPapayas

The first, trivial fix is to just duplicate the console.log('All') in all switch cases:第一个微不足道的解决方法是在所有开关情况下复制console.log('All')

switch (expr) {
  case 'Oranges': console.log('All'); console.log('Oranges'); break;
  case 'Mangoes': console.log('All'); console.log('Mangoes'); break;
  case 'Papayas': console.log('All'); console.log('Papayas');
}

the last break may be omitted.最后的break可以省略。 An alternative that requires the use of a second switch and duplicating the case s is to just execute one switch after another:需要使用第二个switch并复制case s 的另一种方法是只执行一个switch

let val = expr;

switch (val) {
  case 'Oranges':
  case 'Mangoes':
  case 'Papayas':
    console.log('All')
}

switch (val) {
  case 'Oranges': console.log('Oranges'); break;
  case 'Mangoes': console.log('Mangoes'); break;
  case 'Papayas': console.log('Papayas'); break;
}

but I'd question whether a switch is the appropriate language construct to use here .但我会质疑switch是否是在这里使用的合适的语言结构 An if comes to mind:想到一个if

let val = expr;
if (val == 'Oranges' || val == 'Mangoes' || val == 'Papayas') {
  console.log('All');
  switch (val) {
    case 'Oranges': console.log('Oranges'); break;
    case 'Mangoes': console.log('Mangoes'); break;
    case 'Papayas': console.log('Papayas'); break;
  }
}

Ideally, you can look up the message in a dictionary / "object" in JS terminology:理想情况下,您可以在 JS 术语中的字典/“对象”中查找消息:

const messages = {
  Oranges: 'Oranges',
  Mangoes: 'Mangoes',
  Papayas: 'Papayas'
}
let message = messages[expr];
if (message) {
  console.log('All');
  console.log(message);
}

Alternatively using arrow funcs if your code doesn't consist just of a mapping data -> data but rather data -> action:如果您的代码不仅包含映射数据 -> 数据,而是包含数据 -> 操作,也可以使用箭头函数:

const funcs = {
  Oranges: () => console.log('Oranges'),
  Mangoes: () => console.log('Mangoes'),
  Papayas: () => console.log('Papayas')
}
let func = funcs[expr];
if (func) {
  console.log('All');
  func();
}

You could take advantage of the way select statements will continue after matching the first item if a break;如果break; isn't declared.没有声明。 Example below uses conditional breaks to output the following.下面的示例使用条件中断来输出以下内容。

  • Input: 'All' = All - Oranges Mangoes and Papayas输入:'All' = All - Oranges Mangoes and Papayas
  • Input: 'Oranges' = Oranges输入:“橙子”= Oranges
  • Input: 'MangPap' = All - Mangoes and Papayas输入:'MangPap' = All - Mangoes and Papayas
  • Input: 'Mangoes' = Mangoes输入:“芒果”= Mangoes
  • Input: 'Papayas' = Papayas输入:“木瓜”= Papayas
  • Input: unrecognized (default) = Input not recognized.输入:无法识别(默认)= Input not recognized.

 const expr = 'MangPap'; var output = ''; switch (expr) { case 'All': output = 'All - '; case 'Oranges': if ( output.length > 0 ) { output += 'Oranges'; } else { output += 'Oranges'; } if (expr === 'Oranges') { break; } case 'MangPap': if ( output.length > 0 ) { } else { output += 'All - '; } case 'Mangoes': if ( output.length > 0 ) { output += ' Mangoes'; } else { output += 'Mangoes'; } if (expr === 'Mangoes') { break; } case 'Papayas': if ( output.length > 0 ) { output += ' and Papayas'; } else { output += 'Papayas'; } if (expr === 'Papayas') { break; } default: if ( output.length > 0 ) { } else { output += output = 'Input not recognized.'; } } console.log( output );

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch参考: https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM