Search code examples
angularjsangularjs-ng-repeat

angularjs check if key allready exist and if not exist push it to scope inside foreach loop


im geting data from Db using php at first for building a table , later im geting new json data from socket and updating $scope, what i need is to push values that not exist in $scope.

angular.forEach(event.market, (market ,keym) => {

   angular.forEach($scope.users.results, function(value, key) {

   if(value.marketId == keym) // keym = new marketId from socket
   {
     //do nothing
   }
  else
  {
     //push(new valuse);  
  }


   });
});

the problem im having is that if a marketId dosent match any keys that i allready have then it pushes the amount of times it didnt match instead of pushing 1 time if it didnt match. what am i doing wrong?

thanks


Solution

  • that solved my issue :

    angular.forEach(game.market, (market, keym) => {
    
      var addToArray = true;
      for (var i = 0; i < $scope.users.results.length; i++) {
        if ($scope.users.results[i].marketId == keym) {
          addToArray = false;
        }
      }
      if (addToArray) {
    
    
        $scope.users.results.push({
          marketId: keym,
          marketName: market.name
        });
    
      }
    
      $scope.users.results.marketName = ' ';
      $scope.users.results.marketId = ' ';
    
    });
    

    thanks