For looping through an array, the correct result is not returned

I am comparing a variable with an array: $scope.object.id and $scope.groepen.id with an if statement after using the for loop. If $scope.object.id exactly matches one of the identifiers $scope.groepen.id , then it should make this index $scope.overlap true.

I use another if it checks if $scope.overlap is true. If one element of $scope.overlap is true, it will make $scope.bestaand true. Otherwise, it should make it false.

 for (var i = 0; i < $scope.groepen.length; i++) { if ($scope.object.id === $scope.groepen[i].id) { $scope.overlap[i] = true; if ($scope.overlap[i]) { $scope.bestaand = true; } else { $scope.bestaand = false; } } else { $scope.overlap[i] = false; } } 

My console log shows me that $scope.overlap really shows the correct values ​​(therefore, if nothing is the same, all indexes are false). $scope.bestaand returns true if something is the same, but does not return to false.

I use Angular form validation to show if validation works or not shown here:

 <div class="col-md-3" ng-class="{ 'has-error' : bestaand }"> <label class="control-label" for="textinput">Groepsnaam</label> <input id="groepen" name="groepen" type="text" class="form-control input-md" ng-model="object.id" ng-minlength="4" ng-maxlength="16" ng-change="checkOverlap()" required> <p ng-show="bestaand" class="help-block">Deze groepsnaam bestaat al!</p> </div> 

What am I doing wrong here?

Edit:

I changed the place of my if statements. Updated code shown here:

 for (var i = 0; i < $scope.groepen.length; i++) { if ($scope.object.id === $scope.groepen[i].id) { $scope.overlap[i] = true; } else { $scope.overlap[i] = false; } if ($scope.overlap[i]) { $scope.bestaand = true; console.log("works") } else { $scope.bestaand = false; console.log("doesnt work") } } 

The console log shows me the following:

enter image description here

It seems like it is becoming true, but it is being overwritten (I have a value that matches the second value of the array). If I enter a value that matches the LAST value of the array, it works.

+6
source share
3 answers

Your problem is that you enclosed if ($scope.overlap[i]) { inside if ($scope.object.id === $scope.groepen[i].id) { , so $scope.overlap will always be true This means that $scope.bestaand will only be set to true or will remain undefined . What you really want is

 for (var i = 0; i < $scope.groepen.length; i++) { if ($scope.object.id === $scope.groepen[i].id) { $scope.overlap[i] = true; } else { $scope.overlap[i] = false; } if ($scope.overlap[i]) { $scope.bestaand = true; } else { $scope.bestaand = false; } } 

Edit If you want to set $scope.bestaand to true, if any of your $scope.overlap elements is true, you need a slightly different one -

 $scope.bestaand = false; for (var i = 0; i < $scope.groepen.length; i++) { if ($scope.object.id === $scope.groepen[i].id) { $scope.overlap[i] = true; } else { $scope.overlap[i] = false; } if(!$scope.bestaand) { if ($scope.overlap[i]) { $scope.bestaand = true; } } } 
+3
source

This is because the else statement is not available:

 $scope.overlap[i] = true; if ($scope.overlap[i]) { $scope.bestaand = true; } else { console.log('UNREACHABLE'); $scope.bestaand = false; } 

Regarding your edited question:

Your $scope.bestaand indicates an error, so I assume that if it was false, as soon as it was never true.

So, you need to rewrite your loop as follows:

 $scope.bestaand = false; for (var i = 0; i < $scope.groepen.length; i++) { if ($scope.object.id === $scope.groepen[i].id) { $scope.overlap[i] = true; } else { $scope.overlap[i] = false; } if ($scope.overlap[i] && !$scope.bestaand) { $scope.bestaand = true; } } 
+1
source

because there is no case that you set it to false. if ($scope.object.id === $scope.groepen[i].id) true, then you already set it to overlap = true , if the overlap is false, you do not set bastaan = false in any case

0
source

All Articles