0

I have a Collection which has two Keys which like Below AngularJs model.

$scope.Collection=[{id:1,name:"A"},{id:1,name:"B"},{id:1,name:"A"},{id:1,name:"C"},{id:2,name:"A"},{id:2,name:"C"},{id:2,name:"A"},{id:3,name:"D"}];

I want to remove Duplicate rows, if Both Keys have Same Values and wanted Array Without Duplicate rows using AngularJs Filter.

Sample Output Should be like Below

$scope.Collection=[{id:1,name:"A"},{id:1,name:"B"},{id:1,name:"C"},{id:2,name:"A"},{id:2,name:"C"},{id:3,name:"D"}]
  • Have you checked this? http://stackoverflow.com/questions/15914658/how-to-make-ng-repeat-filter-out-duplicate-results – Dario Aug 29 '16 at 11:03
  • SO is not free code giving site. Please post the code that you have tried so far. – Umair Farooq Aug 29 '16 at 11:03
  • http://stackoverflow.com/questions/15914658/how-to-make-ng-repeat-filter-out-duplicate-results – Jitendra Tiwari Aug 29 '16 at 11:26
  • Thanks for All Comments.. Above all links remove duplicates, after Check One Column. But I want to check two Column and if only one column values same, it should Keep. I did the filter for a it and Check Below My Answers. Thanks Again for all your supports. – Ahamed Ahlam Aug 30 '16 at 06:46

2 Answers2

0

A possible solution is this (base angularjs):

.filter('remover', function () {
    return function ( items ) {

        //filtering duplicates...
        var filtered = {};
        for ( var i = 0; i < items.length; i++ ) {
           if(filtered[items[i].id] == undefined)
              filtered[items[i].id] = [];

           if(filtered[items[i].id].indexOf(items[i].name) != -1)
              filtered[items[i].id].push(items[i].name);
        }

        //new array with filtered elements
        var array = [];
        for(var id in filtered){
           array.push({id: id, name: filtered[id]});
        }

        return filtered;
    }        
} );

this should be Θ(2n) because you iterate twice you array: beware to use it with big loads of data.

illeb
  • 2,942
  • 1
  • 21
  • 35
0

This is the Angular filter to Check the Given two Key names and If Both Key names values same in both rows, remove the One row from the return Output Collection.

 .filter('unique2', function () {
    return function (collection, keyname1, keyname2) {
        var output = [],
            keys = [];           

        for (row = 0; row < collection.length; row++) {               
                var num1 = collection[row][keyname1];
                var num2 = collection[row][keyname2];

                for (otherrow = row + 1; otherrow < collection.length; otherrow++) {
                    var otherrow_index = otherrow;
                    if (num1 == collection[otherrow_index][keyname1] && num2 == collection[otherrow_index][keyname2]) {                            
                        collection.splice(otherrow, 1);
                        otherrow--;

                        } 

                    }
        }

        return collection;
    };
});