79 loops node.js underscore.js
node.js中的代码非常简单.
_.each(users, function(u, index) {
if (u.superUser === false) {
//return false would break
//continue?
}
//Some code
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果superUser设置为false,如何在不执行"Some code"的情况下继续下一个索引?
PS:我知道其他条件可以解决问题.仍然很想知道答案.
Pet*_*ons 134
_.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
});
Run Code Online (Sandbox Code Playgroud)
请注意,使用lodash(不是下划线)_.forEach如果你想提前结束"循环",你可以显式地return false从iteratee函数和lodash提前终止forEach循环.