56

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?

AtheistP3ace
  • 9,611
  • 12
  • 43
  • 43
Michael
  • 13,950
  • 57
  • 145
  • 288

3 Answers3

114
return false;

Use this in a lodash each to break.

EDIT: I have seen the 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.

AtheistP3ace
  • 9,611
  • 12
  • 43
  • 43
  • 1
    Any advantage of `return false;` over `return` ? Or just for readability ? – Rayon Oct 21 '15 at 18:57
  • @RayonDabre If its lodash the advantage is breaking the loop. Just using return will not break out of the loop, you must return false to achieve that. – AtheistP3ace Oct 21 '15 at 18:58
  • 5
    @RayonDabre `return` with no parameter is equivalent to `return undefined`, which is the same as falling off the end of the function without using a `return` statement. – Barmar Oct 21 '15 at 19:05
  • @Barmar, So only `return` will return the control flow and break the iterations right ? – Rayon Oct 21 '15 at 19:07
  • @RayonDabre No, return like he said is just returning undefined, only returning false will break the loop and stop any subsequent iterations. – AtheistP3ace Oct 21 '15 at 19:08
  • 7
    @RayonDabre Normally the iteration function doesn't have a `return` statement, so it implicitly returns `undefined`. If returning undefined stopped the iteration, you would never get more than one iteration. That's why it requires an explicit `return false` as the flag to tell it to break. – Barmar Oct 21 '15 at 19:10
  • return; is like continue in this case. – downhand Jun 06 '16 at 14:08
  • what happen if you return **true** instead? – s1moner3d Aug 05 '16 at 14:19
  • @s1moner3d the each loop would continue until you return false or it completes. – AtheistP3ace Aug 05 '16 at 14:20
  • 1
    @AtheistP3ace thank u! That's for covering every case **undefined|true|false** – s1moner3d Aug 05 '16 at 16:39
  • 1
    FYI about the Underscore/LoDash name: https://stackoverflow.com/a/13898916/493807 -- In short, LoDash can be used as a drop-in replacement for Underscore. – Andy Jun 15 '17 at 22:29
24
  • return false => it has exactly the same effect as using break;

  • return => this is the same as using continue;

jose920405
  • 7,982
  • 6
  • 45
  • 71
jack wu
  • 301
  • 4
  • 6
  • It would be better if you explain why your answer would be helpful. – Anh Pham Sep 17 '17 at 05:57
  • 1
    it is better because flow control. the answer shows the difference between not returning anything (which would cause the loop to skip to the next element) or returning false (which would cause the loop to stop alltogether) I learned something today. Thanks @jack wu – salgmachine Jan 26 '18 at 13:20
3

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.

var hasAtLeastOneFalseStatus = _.any($scope.inspectionReviews, function (value, key) {
    return !value.IsNormal;
})

$scope.status = hasAtLeastOneFalseStatus ? false: true;
ncksllvn
  • 5,699
  • 2
  • 21
  • 28