node 中的代码.js非常简单.
_.each(users, function(u, index) {
if (u.superUser === false) {
//return false would break
//continue?
}
//Some code
});
我的问题是,如果superUser设置为false,我如何在不执行"某些代码"的情况下继续下一个索引?
附言:我知道另一种情况可以解决这个问题.仍然好奇地想知道答案.
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 === 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
});
另请注意,使用lodash(不是下划线)_.forEach
,如果您确实希望提前结束"循环",可以显式地从iteratee函数中删除return false
,lodash将提前终止forEach
循环.