Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angularjs

  $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??

like image 418
Bharani Avatar asked Jan 30 '13 09:01

Bharani


1 Answers

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);
     }
}
like image 197
Iftah Avatar answered Oct 16 '22 23:10

Iftah