如何';继续';在每个循环中:下划线,node.js
node.js中的代码非常简单如何';继续';在每个循环中:下划线,node.js,node.js,loops,underscore.js,Node.js,Loops,Underscore.js,node.js中的代码非常简单 _.each(users, function(u, index) { if (u.superUser === false) { //return false would break //continue? } //Some code }); 我的问题是,如果superUser设置为false,我如何在不执行“某些代码”的情况下继续下一个索引 附言:我知道有其他条件可以解决这个问题。我仍然很想知道答案 _.each(users, fun
_.each(users, function(u, index) {
if (u.superUser === false) {
//return false would break
//continue?
}
//Some code
});
我的问题是,如果superUser设置为false,我如何在不执行“某些代码”的情况下继续下一个索引
附言:我知道有其他条件可以解决这个问题。我仍然很想知道答案
_.each(users, function(u, index) {
if (u.superUser) {
//Some code
}
});
请注意,如果要结束“循环”,请使用lodash(非下划线)
\u0.forEach
early您可以从iteratee函数显式地返回false
,lodash将提前终止forEach
循环。而不是for循环中的continue
语句您可以在中使用return
语句。在underline.js中的each()
语句只会跳过当前迭代。对不起。我应该把情节说得很详细。如果超级用户为false,我需要执行一些代码,然后继续。还有另一个条件,比如说,if(superUser!=false&&activated),我需要为其执行其他操作并执行“某些代码”,然后还有其他操作需要执行“某些代码”。我只是想知道是否有一种方法可以做到这一点,而不必在elseif和else中重写相同的代码。我不想为此创建另一个函数。他问如何避免使用非常糟糕的arrow-code。为什么这不适用?因为.each
和一个常规的for(){}
循环不是一回事。@ConAntonakos当你在JS中为each(collection,callback)使用时,callback
内部没有for循环,因此中断/继续不适用。
_.each(users, function(u, index) {
if (u.superUser === false) {
return;
//this does not break. _.each will always run
//the iterator function for the entire array
//return value from the iterator is ignored
}
//Some code
});