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

Rimian
  • 36,864
  • 16
  • 117
  • 117
Bharani
  • 157
  • 1
  • 1
  • 8
  • Possible duplicate of [how to prevent duplicate in array push in angularjs](http://stackoverflow.com/questions/25667253/how-to-prevent-duplicate-in-array-push-in-angularjs) – Dave DuPlantis May 03 '17 at 20:09

1 Answers1

24

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);
     }
}
Iftah
  • 9,512
  • 2
  • 33
  • 45
  • You are welcome :) You can mark the question as 'Answered' by clicking the checkbox (below the voty of the answer). This will save time for other readers who look for unanswered questions, and serve as a 'thank you' to the person who wrote the answer (no need to comment 'thank-you's in this website). – Iftah Jan 30 '13 at 10:13
  • is the line '$scope.yeardisplay=display; ' necessary? just curious? – GMan Apr 09 '14 at 14:46
  • Definitely not necessary, I don't know what's it for. I'm guessing it is displayed somewhere on his page. I was just modifying the push+uniq combo into valid js code and left the rest of it untouched. – Iftah Apr 09 '14 at 19:32
  • FYI, this does not work in IE8 and below because they don't support "indexOf" – b2238488 Apr 27 '14 at 10:15
  • @b2238488 you are right, thanks. For older browsers the same can be accomplished with jQuery's inArray: https://api.jquery.com/jQuery.inArray/ but Angular.js doesn't support IE8 as well so I think its safe to use indexOf for an Angular.js question. – Iftah Apr 27 '14 at 11:08