Method for removing duplicate data in ng repeat of Angularjs

  • 2021-07-09 06:49:00
  • OfStack

This paper illustrates the method of removing duplicate data in ng-repeat of Angularjs. Share it for your reference, as follows:

1. JS:


ngApp.filter('unique', function () {
  return function (collection, keyname) {
    var output = [],
      keys = [];
    angular.forEach(collection, function (item) {
      var key = item[keyname];
      if (keys.indexOf(key) === -1) {
        keys.push(key);
        output.push(item);
      }
    });
    return output;
  };
});

2. Html:


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

I hope this article is helpful to everyone's AngularJS programming.


Related articles: