0

I made a loop in and i want to get a distinct values from this loop then push it to an array.

What I got instead, are repetitive values.

The $scope.Empassignedvacations returns multiple data from datatable from db. one column from it is vac which displays multiple vacations keys in the db. What i want to do is to take these keys and distinct them and push them to another $scope array. its name is $scope.checkedvacs. but i got 2,2,2,2,20,20,20,20

Assignments.getvacations().then(function (response) {
   $scope.vacations = (response.data);

   Assignments.GetEmpassignedvacations($scope.SelectedEmp1.staffkey)
   .then(function (response) {
     $scope.Empassignedvacations = (response.data)
     $scope.checkedvacs.push( $scope.Empassignedvacations.vac );

     angular.forEach($scope.Empassignedvacations, function (e) {
          angular.forEach($scope.AlternateDirector, function (a) {
               if (e.Staff_Key == a.Staff_Key) {
                  $scope.AlternateD = e.AlternateD; 
               }
          })
          angular.forEach($scope.status, function (s) {
               if (e.status == s.stsid) {
                  $scope.sts = s.stsid;
               }
          })
     })

Thanks in advance

0

2 Answers 2

0

It happens because you play with the same value $scope.Empassignedvacations.

You push it to the array and after - change it. This is a reason why you get 2,2,2,2,20,20,20,20

So you can fix it by pushing the copy of the value, like:

$scope.checkedvacs.push( angular.copy($scope.Empassignedvacations.vac));
Sign up to request clarification or add additional context in comments.

Comments

0

First declare $scope.checkedvacs=[] & $scope.keys=[] in globally for standard code practice then try bellow code

angular.forEach($scope.Empassignedvacations.vac, function(item) {
  // we check to see whether our object exists
  $scope.keys = item[keyname];
  // if it's not already part of our keys array
  if ($scope.keys.indexOf(key) === -1) {
    // push this item to our final output array
    $scope.checkedvacs.push(item);
  }
});

remove bellow code and replace your code with first code sample..

$scope.checkedvacs.push( $scope.Empassignedvacations.vac );

 angular.forEach($scope.Empassignedvacations, function (e) {
      angular.forEach($scope.AlternateDirector, function (a) {
           if (e.Staff_Key == a.Staff_Key) {
              $scope.AlternateD = e.AlternateD; 
           }
      })
      angular.forEach($scope.status, function (s) {
           if (e.status == s.stsid) {
              $scope.sts = s.stsid;
           }
      })

You can also follow bellow link that may be helpful for you https://tutorialedge.net/javascript/angularjs/removing-duplicates-from-ng-repeat/

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.