Go to the next value in ForEach - AngularJS

2

Friends, I have the following problem. I have an arrangement with 3 elements which I should consult in a BD. By the time one matches the base records, I need the loop to skip to the next value in the array.

What I have so far is this:

restAPI.query(function (adt) {
        $scope.areaDocumentTypes = adt;
        angular.forEach($scope.collection, function (dt) {
            angular.forEach($scope.areaDocumentTypes, function (item) {
                if ($scope.idArea == item.idArea && dt.idDocumentType == item.idDocumentType) {
                    if (!dt.selected)
                        //Código...
                }
                else
                    if (dt.selected)
                        //Código
            });
        });
    });

I tried a return after the else but it did not work.

Any suggestions are appreciated!

    
asked by Paulo Urbano Rivera 09.03.2017 в 17:31
source

2 answers

0

What you are looking for is to use the "continue" instruction, which is not available in the foreach loop. A possible solution would be to use a flag (or flag) to control the execution:

var continuar= true;
angular.forEach([0,1,2], function(count){
  if(continuar) {
    if(count == 1){
      continuar= false;
    }
  }else
      continuar= true;
});
    
answered by 10.03.2017 / 14:20
source
0
  

Note: Always use keys in instructions even if it's one line. Not using them can end in a typo, for example, if some editor adds or deletes a space / tab by mistake.

To simulate a continue in loops forEach simply use return to avoid the current iteration:

angular.forEach($scope.collection, function (dt) {
  if ($scope.idArea == item.idArea && dt.idDocumentType == item.idDocumentType) {
    if (!dt.selected) {
      // código normal
    }
    else {
      if (dt.selected) {
        return; // salta la interación actual en este punto
      }
    }
});
    
answered by 10.03.2017 в 14:29