2

I am trying to get a category dropdown list fetched from below JSON data. I have same category multiple times, For Ex: Computer

[
{ 
"title":"C Programming", 
"category":"Computer"
},
{ 
"title":"International Tax", 
"category":"Business"
},
{ 
"title":".net Programming", 
"category":"Computer"
}
//more data...
]

AngularJS:

function showSubjects($scope, $http)
{
$http({method: 'POST', url: 'js/subjects.json'}).success(function(data)
{
$scope.items= data; // response data 
});
}

HTML:

<div id="ng-app" ng-app ng-controller="showSubjects">
<select>
<option ng-repeat="subjects in items">{{subjects.category}}</option>
</select>
</div>

I want to display duplicate categories only once. Please give me some suggestions, how to acheive the required output. Thanks in advance.

user2218497
  • 373
  • 1
  • 4
  • 8
  • 1
    Duplicate of http://stackoverflow.com/questions/15914658/angular-js-how-to-make-ng-repeat-filter-out-duplicate-results – Gruff Bunny Apr 30 '14 at 09:05

2 Answers2

2

Use

<option ng-repeat="subjects in items | unique:'category'">{{subjects.category}}</option>

Look here for more: Unique & Stuff

Omri Aharon
  • 16,959
  • 5
  • 40
  • 58
0

You can use your own function like below:

   <select name="cmpPro" ng-model="test3.Product" ng-options="q for q in productArray track by q">
    <option value="" >Plans</option>
</select>

productArray =[];
angular.forEach($scope.leadDetail, function(value,key){
var index = $scope.productArray.indexOf(value.Product);
if(index === -1)
{
    $scope.productArray.push(value.Product);
}
});
Akhilesh Kumar
  • 849
  • 1
  • 15
  • 28
  • @UI_Dev : I am already using this code in 2 projects. May be it is not working for you. Answer given by Omri Aharon is good approach. – Akhilesh Kumar Dec 28 '18 at 06:29