Remove Duplicates From Array in AngularJs


Remove Duplicates From Array in AngularJs : Duplicate values in any array sometimes creates problem so we need some check which can remove the duplicate values from AngularJs Array. We can use pure javascript filter method to remove duplicate values. Here in this tutorial we are going to explain how you
can remove duplicate values from an array. You can also use our online editor to edit and run the code online.


Remove Duplicates From Array in AngularJs

First we need an array which have duplicate values so we will create an array with duplicate values and then process to remove duplicate values.

Remove Duplicates From Array in AngularJs Example:


<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
$scope.exampleArray = ['100','98','98','88','101','102','102'];
$scope.result = [];
 $scope.removeDuplicates = function() {
     $scope.result = $scope.exampleArray.filter(function(item, pos) {
     return $scope.exampleArray.indexOf(item) == pos;
     })
 };
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController"> 
 Array With Duplicate Values = {{exampleArray}}
 Click to Remove Duplicates Array .
 <button ng-click='removeDuplicates()' >Remove Duplicates</button><br>
 Final Array = {{result}}<br>

</div> 
</div>
</body>  
</html>      
                                                                                    

Try it »

If you run the above example it will remove the duplicate values from Array. The output of the above example will be something like this –

Remove Duplicates From Array in AngularJs


Advertisements

Add Comment

📖 Read More