Angularjs Deduplication


#1

There are duplicate values ​​in the array. I want to output only one of the duplicate values.
Is there a good way?

There is a problem with the previous code and it is being edited.

$scope.items = [
  {"id" : 1 , "dddd" : "name_1"},
  {"id" : 1 , "aaa" : "name_2"},
  {"id" : 3 , "vwwe" : "name_3"}
]

z.x.push( { “id”: id , “name”: name } );

<div ng-repeat="x in p">
        <div>{{x.id}}</div>   
            <div>{{x.name}}</div>
        </div>
</div>

out view

id: 1
dddd
aaa
id 3
vwwe


#2

<div ng-repeat= " x in p | groupBy:'id' "> or make a filter like this

.filter('unique', function() {
    return function(collection, primaryKey) { //no need for secondary key
      var output = [], 
          keys = [];
          var splitKeys = primaryKey.split('.'); //split by period


      angular.forEach(collection, function(item) {
            var key = {};
            angular.copy(item, key);
            for(var i=0; i<splitKeys.length; i++){
                key = key[splitKeys[i]];    //the beauty of loosely typed js :)
            }

            if(keys.indexOf(key) === -1) {
              keys.push(key);
              output.push(item);
            }
      });

      return output;
    };
})

then…

<div ng-repeat= " x in p | unique: 'id' ">


#3

Thank you.
I tried filtering but the second value is not output. I’ll try again.


#4

Thank you.
I tried filtering but the second value is not output. I’ll try again.


#5

please be more precise or show the result to make yourself understandable to others also…


#6

I used ng-repeat twice to fix the problem.
Thank you very much.