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 ?
            Asked
            
        
        
            Active
            
        
            Viewed 38 times
        
    -4
            
            
        
        ojus kulkarni
        
- 1,877
 - 3
 - 25
 - 41
 
- 
                    3Please kindly search in SO before asking a question. – Win Feb 23 '16 at 20:02
 - 
                    I didn't get the proper answer in SO without filter – ojus kulkarni Feb 23 '16 at 20:03
 - 
                    1Here is how you search in Google for SO - [site:stackoverflow.com javascript check array contains](https://www.google.com/webhp?&ie=UTF-8#q=site:stackoverflow.com+javascript+check+array+contains) – Win Feb 23 '16 at 20:04
 - 
                    have you try indexOf method ? – Sherlocked Nguyen Feb 23 '16 at 20:05
 - 
                    1[SO post: determine-whether-an-array-contains-a-value](http://stackoverflow.com/questions/1181575/determine-whether-an-array-contains-a-value) – Michael Feb 23 '16 at 20:06
 - 
                    @SherlockedNguyen can you please explain it with one example? – ojus kulkarni Feb 23 '16 at 20:07
 
1 Answers
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