简体   繁体   English

Angular:如何从内循环中脱离外循环?

[英]Angular: How to break from outer loop from inner loop?

I tried using break Statement. 我尝试使用break语句。 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;
                    }

                });

When you use forEach , there is no way to pause iteration - not even by using return . 使用forEach ,无法暂停迭代-甚至无法使用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). 如果您想在循环的中间停止迭代,则必须使用实际的forwhile循环(可能类似于for..infor..of或传统的三态for(var i = 0; i < whatever; i++如果myfilejob是一个数组for(var i = 0; i < whatever; i++循环myfilejob )。

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?) (不要介意您尝试添加的break语句无论如何都在外部循环中-从此示例中删除标签将使该功能等效。您是否要重构(count > 1)检查以使其在内部正常工作内循环?)

You can use return, which will essentially break out of the loop. 您可以使用return,这实际上会跳出循环。 If you want to do it within the inner loop, set a boolean to true, and check for it in the outer loop. 如果要在内部循环中执行此操作,请将布尔值设置为true,然后在外部循环中进行检查。

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

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 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM