Javascript 如何停止 lodash.js _.each 循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33266946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop lodash.js _.each loop?
提问by Michael
I have this rows code:
我有这个行代码:
_.each($scope.inspectionReviews, function (value, key) {
alert("status=" + value.IsNormal + " " + "name=" + value.InspectionItemName);
if (!value.IsNormal) {
$scope.status = false;
return;
}
$scope.status = true;
})
At some point I want to stop looping but it seems that return not working.
在某些时候我想停止循环,但似乎返回不起作用。
How can I stop the loop?
我怎样才能停止循环?
回答by AtheistP3ace
return false;
Use this in a lodash each to break.
在 lodash 中使用它来破坏。
EDIT: I have seen title changed to underscore. Is it underscore or lodash? As I pointed out above you can break an each in lodash but underscore I believe emulates forEach which natively doesn't provide that.
编辑:我看到标题改为下划线。它是下划线还是lodash?正如我上面指出的,您可以在 lodash 中打破 each ,但下划线我相信模拟 forEach 本身不提供。
回答by Hyman wu
return false=> it has exactly the same effect as using
break;
return=> this is the same as using
continue;
return false=> 它与使用的效果完全相同
break;
return=> 这与使用相同
continue;
回答by ncksllvn
If you want to test to see if a certain condition is true for any of the collection's members, use Underscore's some(aliased as any) instead of each
.
如果您想测试某个集合的任何成员是否满足某个条件,请使用 Underscore 的some(别名为any)而不是each
。
var hasAtLeastOneFalseStatus = _.any($scope.inspectionReviews, function (value, key) {
return !value.IsNormal;
})
$scope.status = hasAtLeastOneFalseStatus ? false: true;