在 Coffeescript 中中断/继续嵌套 for 循环
如何在 Coffeescript 中中断/继续嵌套循环?例如,我有类似的内容:
for cat in categories
for job in jobs
if condition
do(this)
## Iterate to the next cat in the first loop
另外,有没有办法将整个第二个循环包装为第一个循环中另一个函数的条件?例如
for cat in categories
if conditionTerm == job for job in jobs
do(this)
## Iterate to the next cat in the first loop
do(that) ## Execute upon eliminating all possibilities in the second for loop,
## but don't if the 'if conditionTerm' was met
How can I break/continue nested loops in Coffeescript? E.g. I have something like:
for cat in categories
for job in jobs
if condition
do(this)
## Iterate to the next cat in the first loop
Also, is there a way to wrap the whole second loop as a conditional to another function within the first loop? E.g.
for cat in categories
if conditionTerm == job for job in jobs
do(this)
## Iterate to the next cat in the first loop
do(that) ## Execute upon eliminating all possibilities in the second for loop,
## but don't if the 'if conditionTerm' was met
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
break
就像 js 一样工作:你的第二种情况不是很清楚,但我假设你想要这个:
break
works just like js:Your second case is not very clear, but I assume you want this:
使用标签。由于 CoffeeScript 不支持它们,因此您需要这样进行破解:
Use labels. Since CoffeeScript doesn't support them, you need to hack as such:
Coffescript 的“break”仅中断立即循环,无法识别外部循环是否中断(烦人!)。在某些情况下,以下黑客可以在满足条件时打破多个循环:
Coffescript's "break" only breaks the immediate loop and has no way of identifying an outer loop for breakage (annoying!). This following hack works in some instances for breaking out of multiple loops when a condition is met:
使用带有 return 的匿名循环
Using anonymous loop with return
Coffeescript 永远不会有多个中断/继续语句,您必须坚持使用丑陋且过多的标志来污染您的代码,或者尝试用 lambda 替换它,并使用
return
作为多重休息。https://github.com/jashkenas/coffeescript/issues/4254
Coffeescript would never have multiple breaking/continuing statements, you have to stick to ugly and excessive flags polluting your code or try to replace it by
do
with a lambda and usereturn
as a multiple break.https://github.com/jashkenas/coffeescript/issues/4254
为了检查数组中的所有元素,也许 lodash 的
every
会有用?https://lodash.com/docs#every
For checking all elements in an array, maybe lodash's
every
would be of use?https://lodash.com/docs#every
我想如果你想使用内部中断/继续,你的代码设计不是很好。
在我看来,任何编程语言都不允许这样做。
按照某人的建议使用标签也被认为是不好的风格。
I suppose the design of your code is not very good if you want to use inner break/continue.
It seems to me that any programming language doesn`t allow that.
Using labels as someone suggested is also considered as bad style.