-4

I have array of name:
$scope.myArray with some values.
Now I want to check an Item is present in array or not,
If it is not present then I want to push that item into array and If that item is already in array then I don't want to push it.
Please tell me how to do this ?

ojus kulkarni
  • 1,877
  • 3
  • 25
  • 41

1 Answers1

0
if($scope.myArray.indexOf(item) === -1) {
    $scope.myArray.push(item)
}

You can read about indexOf for more details. Also, checkout underscore js for some readily available functions.

Aniket Sinha
  • 6,001
  • 6
  • 37
  • 50