0

I tried using break Statement. but didnt worked out. I wanted to break out from outer loop as soon as some condition matches in inner loop.

     angular.forEach(myfilejob, function(fieldMapO) {
                    var count = 0;
                    angular.forEach(myfilejob, function(fieldMapI) {
                        if(fieldMapO.key == fieldMapI.key){
                            $scope.dupKey = fieldMapI.key;
                            count ++;
                        }
                    });

                    if(count>1){
                        $scope.dups = true;
                        // tried break , but didnt work

                    }
                    else{
                        $scope.dups = false;
                    }

                });
5
  • Generally speaking, forEach calls with a function require you to return false (or some other value) to tell the iterator to exit. I can't imagine angular performs any differently. Commented Mar 21, 2016 at 19:53
  • angular.forEach doesn't support break; statement, to get it support you need to fallback on older version simple for loop. Commented Mar 21, 2016 at 19:57
  • Why are you looping over the same set of data for each piece of data in said set? surely there's a better way, Commented Mar 21, 2016 at 19:58
  • I craeted a table and having a dropdown option on each row. So the count value wl e 2 if user has selects an option from dropdown option more than once. Commented Mar 21, 2016 at 20:01
  • The spot where you tried to put your break statement is in your outer loop - did you mean to put that if/else in your inner loop? (Not that this would solve your problem outright) Commented Mar 21, 2016 at 20:27

3 Answers 3

2

When you use forEach, there is no way to pause iteration - not even by using return. If you want a way to stop iteration in the middle of your loop, you must use an actual for or while loop (probably something like for..in or for..of or a traditional three-statement for(var i = 0; i < whatever; i++ loop if myfilejob is an array).

However, once you have done that, you can then label the loops to get the exact effect you're looking for:

outerDupLoop:
for (var outerIndex in myfilejob) {
    var fieldMapO = myfilejob[outerIndex];
    var count = 0;
    innerDupLoop:
    for (var innerIndex in myfilejob) {
        var fieldMapI = myfilejob[innerIndex];
        if(fieldMapO.key == fieldMapI.key){
           $scope.dupKey = fieldMapI.key;
           count ++;
        }
    });

    if(count>1){
        $scope.dups = true;
        break outerDupLoop; // break from the outer loop
    } else {
       $scope.dups = false;
    }
}

(Never mind that the break statement you were trying to add was in the outer loop anyway - removing the labels from this example would make this functionally equivalent. Did you mean to refactor the (count > 1) check so that it would work properly inside the inner loop?)

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I agree.. i opted the same way and it worked out. Thanks
0

You can use return, which will essentially break out of the loop. If you want to do it within the inner loop, set a boolean to true, and check for it in the outer loop.

var dupsExist = false;
angular.forEach(myfilejob, function(fieldMapI) {
    if(fieldMapO.key == fieldMapI.key){
        $scope.dupKey = fieldMapI.key;
        dupsExist = true;
        return;
    }
});
if (dupsExist){
    return;
}

Comments

0

I suggest you define a bool when your inner condition is satisfied

angular.forEach(myfilejob, function(fieldMapO) {
                var count = 0;
                var conditionFlag = false;
                angular.forEach(myfilejob, function(fieldMapI) {
                    if(fieldMapO.key == fieldMapI.key){
                        $scope.dupKey = fieldMapI.key;
                        count ++;
                        //condition is fulfilled so boolean is true now
                        conditionFlag = true
                        return false
                    }
                });


                if(count>1){
                    $scope.dups = true;

                }
                else{
                    $scope.dups = false;
                }
                //check if condition is satisfied then exit outer loop too
                if(conditionFlag){
                    return false;
                }
            });

Hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.