How to make ng-repeat filter out duplicate results

前端 未结 16 2941
鱼传尺愫
鱼传尺愫 2020-11-22 06:52

I\'m running a simple ng-repeat over a JSON file and want to get category names. There are about 100 objects, each belonging to a category - but there are only

16条回答
  •  没有蜡笔的小新
    2020-11-22 07:13

    I decided to extend @thethakuri's answer to allow any depth for the unique member. Here's the code. This is for those who don't want to include the entire AngularUI module just for this functionality. If you're already using AngularUI, ignore this answer:

    app.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

    Example

提交回复
热议问题