Jascination Jascination - 2 years ago 154
AngularJS Question

How to make ng-repeat filter out duplicate results

I'm running a simple

over a JSON file and want to get category names. There are about 100 objects, each belonging to a category - but there are only about 6 categories.

My current code is this:

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

The output is 100 different options, mostly duplicates. How do I use Angular to check whether a
already exists, and not create an option if it's already there?

edit: In my javascript,
$scope.places = JSON data
, just to clarify

Answer Source

You could use the unique filter from AngularUI (source code available here: AngularUI unique filter) and use it directly in the ng-options (or ng-repeat).

<select ng-model="orderProp" ng-options="place.category for place in places | unique:'category'">
    <option value="0">Default</option>
    // unique options from the categories
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download