如何打破 JavaScript 中的嵌套循环?
我试过这个:
for(i = 0; i < 5; i++){
for(j = i + 1; j < 5; j++){
break(2);
}
alert(1);
}
只是为了得到:
语法错误
:语句前缺少;
那么,如何打破 JavaScript 中的嵌套循环呢?
I tried this:
for(i = 0; i < 5; i++){
for(j = i + 1; j < 5; j++){
break(2);
}
alert(1);
}
only to get:
SyntaxError
: missing;
before statement
So, how would I break a nested loop in JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您应该能够打破标签,如下所示:
You should be able to break to a label, like so:
您需要命名您的外循环并打破该循环,而不是您的内循环 - 像这样。
You need to name your outer loop and break that loop, rather than your inner loop - like this.
至少有五种不同的方法可以打破两个或多个循环:
1) 将父循环设置到末尾
2) 使用标签
3)使用变量
4) 使用自执行函数
5) 使用常规函数
There are at least five different ways to break out of two or more loops:
1) Set parent(s) loop to the end
2) Use label
3) Use variable
4) Use self executing function
5) Use regular function
参见亚伦的。否则:
j=5;i=5
而不是break
。See Aaron's. Otherwise:
j=5;i=5
instead ofbreak
.代码复制自 打破嵌套循环的最佳方法在 Javascript 中?
请在发布问题之前进行搜索。该链接是我在此页面左侧看到的第一个相关问题!
code copied from Best way to break from nested loops in Javascript?
Please search before posting a question. The link was the FIRST related question I saw on the left side of this page!
不幸的是,您必须设置一个标志或使用标签(想想老式的 goto 语句)
标签方法如下所示:
编辑:标签放置不正确。
另请参阅:
Unfortunately you'll have to set a flag or use labels (think old school goto statements)
The label approach looks like:
edit: label incorrectly placed.
also see:
在我看来,将构建词汇量保持在最低限度非常重要。如果我可以取消休息并轻松地继续,我就会这样做。
请注意,循环之后,m 和 k 比您想象的要大。这是因为 m++ 和 k++ 在其循环条件之前执行。然而,它仍然比“脏”休息要好。
编辑:长评论@Dennis ...
我并不是百分百认真地对待“肮脏”,但我仍然认为“中断”违反了我自己的干净代码概念。一想到要进行多层次的休息,我实际上就想洗个澡。
我发现我对代码的感受是合理的,因为我一生都在编码。我能想到的最好的原因是礼仪和语法的结合。休息是不礼貌的。多层次的休息简直就是粗鲁。
当查看 for 语句时,读者确切地知道该看哪里。您需要了解的有关参与规则的所有内容都在合同的括号之间。作为一名读者,休息侮辱了我,感觉就像我被欺骗了。
清晰比欺骗更尊重。
In my opinion, it's important to keep your construct vocabulary to a minimum. If I can do away with breaks and continues easily, I do so.
Be warned, after the loop, m and k are one larger that you might think. This is because m++ and k++ are executed before their loop conditions. However, it's still better than 'dirty' breaks.
EDIT: long comment @Dennis...
I wasn't being 100% serious about being 'dirty', but I still think that 'break' contravenes my own conception of clean code. The thought of having multi-level breaks actually makes me feel like taking a shower.
I find justifying what I mean about a feeling about code because I have coded all life. The best why I can think of it is is a combination of manners and grammar. Breaks just aren't polite. Multi level breaks are just plain rude.
When looking at a for statement, a reader knows exactly where to look. Everything you need to know about the rules of engagement are in the contract, in between the parenthesis. As a reader, breaks insult me, it feels like I've been cheated upon.
Clarity is much more respectful than cheating.
使用函数进行多级循环 - 这是好方法:
Use function for multilevel loops - this is good way:
包装一个自执行函数并返回
Wrap in a self executing function and return
您
返回
以“打破”嵌套的for
循环。You
return
to "break" you nestedfor
loop.break
不带参数。有两种解决方法:将它们包装在函数中并调用
return
在内循环,如果设置了标志,则在循环后立即再次中断。
break
doesn't take parameters. There are two workarounds:Wrap them in a function and call
return
Set a flag in the inner loop and break again right after the loop if the flag is set.
打破第一个循环:
打破两个循环:
Break 1st loop:
Break both loops:
您可以使用“break”一词来中断嵌套的 for 循环,它无需任何标签即可工作。
在您的情况下,您需要有一个足以打破循环的条件。
下面是一个示例:
它记录以下内容:
return; 语句在这种情况下不起作用。
工作笔
You can break nested for loops with the word 'break', it works without any labels.
In your case you need to have a condition which is sufficient to break a loop.
Here's an example:
It logs the following:
The return; statement does not work in this case.
Working pen