Remove Duplicates value from Array using AngularJS

Remove Duplicates value from Array using AngularJS

how to prevent duplicate in array push in angularjs

First we need an al the array check which have duplicate values available or not so we will create an array other array with duplicate values and then this array process to remove all the duplicate values.Remove Duplicates value from Array using AngularJS -angularjs unique array

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<title>Remove Duplicates From Array in AngularJs Example</title>
 <script src="angular.min.js"></script>
 <script>
var liveApp = angular.module("liveApp", []);
 liveApp.controller("liveCtrl", function($scope) {
$scope.ageArrayval = ['25','44','98','44','50','71','50'];
$scope.dataresult = [];
 $scope.deleteDuplicate = function() {
     $scope.dataresult = $scope.ageArrayval.filter(function(item, pos) {
     return $scope.ageArrayval.indexOf(item) == pos;
     })
 };
});
</script>
</head>
<body>  
<div ng-app="liveApp">  
	<div ng-controller="liveCtrl"> 
	 Array With Duplicate Values = {{ageArrayval}}
	 Click to simple Remove all the Duplicates Array .
	 <button ng-click='deleteDuplicate()' >Remove your Duplicates Value</button><br>
	 Result : Final Array = {{dataresult}}<br>

	</div> 
</div>

<a href="https://infinityknow.com" target="_blank" title="Download,free download,steps download,download quickly,easy download,steps download,all download" alt="Download,free download,steps download,download quickly,easy download,steps download,all download" class="download">Example</a>


</body>  
</html> 

AngularJs Remove duplicate elements in ng-repeat Example

Just simple create a filter with remove duplicate id that gets the unique values

READ :  custom ng-template Directive using AngularJS Examples

<div ng-repeat="item in items | datauniq: 'id'"></div>

app.filter('datauniq', function() {
   return function(datacol, keyname) {
      var dataout = [], 
          datakeys = [];

      angular.forEach(datacol, function(item) {
          var dk = item[keyname];
          if(datakeys.indexOf(dk) === -1) {
              datakeys.push(dk);
              dataout.push(item);
          }
      });

      return dataout;
   };
});

Example

Add a Comment

Your email address will not be published. Required fields are marked *