[英]Break or continue loop within function calls
My question is: How can I trigger a break;
我的问题是:我该如何触发break;
or continue;
或continue;
for a loop through a function that gets called? 通过调用一个函数的循环? The thing is, I do not want to nest all possible conditions within a loop to avoid code complexity. 问题是,我不想将所有可能的条件嵌套在循环中,以避免代码复杂。 I was thinking about some pseudo example: 我在考虑一些伪示例:
var someObjects = [...] //array with some js objects
for (var i = 0; i < someObjects.length; i++) {
var element = someObjects[i];
doSomething(element);
}
function doSomething(object) {
//some code here
if (somethingIsDefined) {
doSomethingElse(object);
}
}
function doSomethingElse(anotherObject) {
//some more code here
if (somethingMatches) {
//here i would like to break the most upper loop
}
}
//someObjects will be processed
I know that it would be possible to introduce for example a boolean variable and check within the loop if it is true or false and depending on that, break;
我知道可以引入一个布尔变量,并在循环中检查是真还是假,并以此break;
or continue;
或continue;
. 。
But this - even if it is just one line - would increase the nesting. 但这(即使只是一行)也会增加嵌套。 Do you see any possible solutions? 您看到任何可能的解决方案吗?
If you are using the Underscore library , which I recommend, you could write the following: 如果您使用的是我建议的Underscore库 ,则可以编写以下内容:
_.any(someObjects, doSomething);
function doSomething(object) {
//some code here
return somethingIsDefined &&
doSomethingElse(object);
}
function doSomethingElse(anotherObject) {
//some more code here
return somethingMatches;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.