Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何';继续';在每个循环中:下划线,node.js_Node.js_Loops_Underscore.js - Fatal编程技术网

如何';继续';在每个循环中:下划线,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

node.js中的代码非常简单

_.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
});