Count of unique items in a list


#1

Scenario: I have an array that contains a list of drugs, with some drugs listed multiple times for different indications. I display these in an ion-list and use angular-filter ‘unique’ to only list each drug once. ng-repeat looks like this: ng-repeat="code in codes | unique: 'Drug' .... (the ng-repeat contains other details, including href that opens a details page.

I want to then be able to display a count of how many entries there are for that drug (e.g. drugA - 3, drugB - 2). This will be displayed in a badge in the list item.

Work to date: I can get a count of each drug using $scope.content = $filter('countBy')($scope.codes, 'Drug'); This returns an object in the form {drugA: Count of drugA, drugB: count of drugB…}

Question: How can I combine both the list of unique drug names AND count of each drug in the one list. (Note: when the list is clicked it will show the details for each entry of that drug).

Thanks for any tips to send me in the right direction.