How to make ng-repeat filter out duplicate results

前端 未结 16 2668
鱼传尺愫
鱼传尺愫 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:20

    If you want to list categories, I think you should explicitly state your intention in the view.

    <select ng-model="orderProp" >
      <option ng-repeat="category in categories"
              value="{{category}}">
        {{category}}
      </option>
    </select>
    

    in the controller:

    $scope.categories = $scope.places.reduce(function(sum, place) {
      if (sum.indexOf( place.category ) < 0) sum.push( place.category );
      return sum;
    }, []);
    
    0 讨论(0)
  • 2020-11-22 07:21

    You can use 'unique'(aliases: uniq) filter in angular.filter module

    usage: colection | uniq: 'property'
    you can also filter by nested properties: colection | uniq: 'property.nested_property'

    What you can do, is something like that..

    function MainController ($scope) {
     $scope.orders = [
      { id:1, customer: { name: 'foo', id: 10 } },
      { id:2, customer: { name: 'bar', id: 20 } },
      { id:3, customer: { name: 'foo', id: 10 } },
      { id:4, customer: { name: 'bar', id: 20 } },
      { id:5, customer: { name: 'baz', id: 30 } },
     ];
    }
    

    HTML: We filter by customer id, i.e remove duplicate customers

    <th>Customer list: </th>
    <tr ng-repeat="order in orders | unique: 'customer.id'" >
       <td> {{ order.customer.name }} , {{ order.customer.id }} </td>
    </tr>
    

    result
    Customer list:
    foo 10
    bar 20
    baz 30

    0 讨论(0)
  • 2020-11-22 07:28

    It seems everybody is throwing their own version of the unique filter into the ring, so I'll do the same. Critique is very welcome.

    angular.module('myFilters', [])
      .filter('unique', function () {
        return function (items, attr) {
          var seen = {};
          return items.filter(function (item) {
            return (angular.isUndefined(attr) || !item.hasOwnProperty(attr))
              ? true
              : seen[item[attr]] = !seen[item[attr]];
          });
        };
      });
    
    0 讨论(0)
  • 2020-11-22 07:30

    If you want to get unique data based on the nested key:

    app.filter('unique', function() {
            return function(collection, primaryKey, secondaryKey) { //optional secondary key
              var output = [], 
                  keys = [];
    
              angular.forEach(collection, function(item) {
                    var key;
                    secondaryKey === undefined ? key = item[primaryKey] : key = item[primaryKey][secondaryKey];
    
                    if(keys.indexOf(key) === -1) {
                      keys.push(key);
                      output.push(item);
                    }
              });
    
              return output;
            };
        });
    

    Call it like this :

    <div ng-repeat="notify in notifications | unique: 'firstlevel':'secondlevel'">
    
    0 讨论(0)
提交回复
热议问题