简体   繁体   English

从 Javascript 中的 switch case 内部中断 for 循环

[英]Break for loop from inside of switch case in Javascript

What command I must use, to get out of the for loop, also from //code inside jump direct to //code after我必须使用什么命令才能退出 for 循环,也可以从//code inside直接跳转到//code after

//code before
for(var a in b)
    {
    switch(something)
        {
        case something:
            {
            //code inside
            break;
            }
        }
    }
//code after

use another variable to flag when you need to exit:需要退出时使用另一个变量来标记:

 var b = { firstName: 'Peter', lastName: 'Smith' }; var keepGoing = true; for (var a in b) { switch (true) { case 1: keepGoing = false; break; } if (!keepGoing) break; console.log('switch end'); } console.log('for end');

example例子

You can use label .您可以使用label Have a labeled statement and break to that label.有一个带标签的声明并打破该标签。 outerLoop is the label I have used here. outerLoop是我在这里使用的标签。

//code before
outerLoop:
for (var a in b) {
    switch (something) {
        case 'case1':
            //code inside
            break outerLoop;
    }
}
//code after

Unfortunately, Javascript doesn't have allow break ing through multiple levels.不幸的是,Javascript 不允许break多个级别。 The easiest way to do this is to leverage the power of the return statement by creating an anonymous function:最简单的方法是通过创建匿名函数来利用return语句的强大功能:

//code before
(function () {
    for (var a in b) {
        switch (something) {
        case something:
            {
                //code inside
                return;
            }
        }
    }
}());
//code after

This works because return leaves the function and therefore implicitly leaves the loop, moving you straight to code after这是有效的,因为return离开了函数,因此隐式地离开了循环, code after直接移动到code after


As pointed out in the comments, my above answer is incorrect and it is possible to multi-level break ing, as in Chubby Boy's answer , which I have upvoted.正如评论中指出的那样,我上面的答案是不正确的,并且可以进行多级break ,就像我已投票赞成的Chubby Boy 的答案一样。

Whether this is wise is, from a seven-year-later perspective, somewhat questionable.这是否明智,从七年后的角度来看,有些值得怀疑。

it depends on what you want to accomplish... one thing I commonly do is something like this:这取决于你想完成什么......我经常做的一件事是这样的:

    //code before
for(var a in b)
{
    var breakFor = false;
    switch(something)
    {
        case something:
        {
            //code inside
            breakFor = true;
            break;
        }
    }
    if (breakFor)
        break;
}
//code after

You can tell which loop / switch to break.您可以判断要中断哪个循环/开关。

function foo ()
{
    dance:
    for(var k = 0; k < 4; k++){
        for(var m = 0; m < 4; m++){
            if(m == 2){
                break dance;
            }
        }
    }
}

See this answer .看到这个答案

for(var i=0; i<b.length; i++) {
   switch (something) {
       case 'something1':
           i=b.length;
   }
}

When the iteration variable i reaches the end of the loop, it breaks.当迭代变量i到达循环末尾时,它会中断。 But one can force the iteration variable to reach the end of the loop.但是可以强制迭代变量到达循环的末尾。

I always find using conditional statements one of the easiest ways to control the code flow, at least conceptually.我总是发现使用条件语句是控制代码流的最简单方法之一,至少在概念上是这样。

var done = false;
//code before for loop
for(var a in b){
    switch(switchParameter){
        case firstCase:
            //code inside
            done = true;
            break;
        }
    }
    if(done)
        break;
}
//code outside of for loop

Replace your switch with a series of if statements.用一系列if语句替换您的switch

for (const a of b) {
  if (something === someValue) {
    // code inside
    break; // break out of the for loop
  } else if (something === someOtherValue) {
    // etc.
  }
}

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

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