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

[php]

Remove Duplicates From Array in AngularJs Example
http://angular.min.js

READ :  Angular ng-click Directive in Controller

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;
})
};
});

Array With Duplicate Values = {{ageArrayval}}
Click to simple Remove all the Duplicates Array .

Result : Final Array = {{dataresult}}

Example

[/php]

AngularJs Remove duplicate elements in ng-repeat Example

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

[php]

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;
};
});
[/php]

Example

Leave a Comment