1

there is this thing who i am not figuring out, i have a controller that return me an array of products propriety, that populate a field, so far i have done this:

     $scope.categoryForFilter=[];
//product load
ProductService.listProducts().then(function(data) {
    $scope.products = {
        count: data.count,
        list: data.aaData,
        length: data.aaData.length
    };
    $scope.products.list.forEach(function (element) {
            $scope.categoryForFilter.push({"id": element.category.id, "label": element.category.label})
    });
});

but in this way i have multiple element.category.label in the options field that in my view is the following:

<select ng-model="categoryForFilter" ng-options="cat.label for cat in categoryForFilter" st-search="cat.label">
                        <option></option>
</select>

so how can i filter the results who have the same value in the array? anyone can help please?

Nonsono Statoio
  • 111
  • 3
  • 16

2 Answers2

1

You can use the unique filter, which is part of Angular UI.

You can also use this filter using lodash:

app.filter('unique', function() {
    return function (arr, field) {
        return _.uniq(arr, function(a) { return a[field]; });
    };
});

Source

Community
  • 1
  • 1
Hartger
  • 329
  • 1
  • 14
1

There is a real easy way to this by using lib like lodash with the function "uniq":

$scope.categoryForFilterWithUniqLabel = _.uniq($scope.categoryForFilter, 'label');
Pollux
  • 21
  • 6