Bharani
Bharani

Reputation: 157

How to check uniquness while pushing values into array using Angular JS?

  $scope.displayyears = [];
  $scope.Joinyear = function(display) {
    $scope.yeardisplay = display;       
    $scope.yeardisp = $scope.displayyears.push($scope.yeardisplay);
    $scope.displayyearss = uniq($scope.yeardisp)
  }

it throws error like "uniq is undefined"..How we check uniqueness??

Upvotes: 13

Views: 19961

Answers (1)

Iftah
Iftah

Reputation: 9572

Try checking if the yeardisplay is already in the array before you add it

$scope.displayyears = [];
$scope.Joinyear=function(display){
     $scope.yeardisplay=display;        
     if ($scope.displayyears.indexOf(display) == -1) {
         $scope.displayyears.push(display);
     }
}

Upvotes: 24

Related Questions