0

The data in $scope.filteredShows looks like this:

[{"id":19,"show_name":"The Walking Dead","show_type":"Series","show_date":"Sun, Feb 26 2017","orig_date":"2017-02-26","season":"7","ep_val":"11","episode":"Season 7, Episode 11","watched":false},

{"id":20,"show_name":"The Walking Dead","show_type":"Series","show_date":"Sun, Mar 5 2017","orig_date":"2017-03-05","season":"7","ep_val":"12","episode":"Season 7, Episode 12","watched":false},

{"id":21,"show_name":"The Walking Dead","show_type":"Series","show_date":"Sun, Mar 12 2017","orig_date":"2017-03-12","season":"7","ep_val":"13","episode":"Season 7, Episode 13","watched":false},

{"id":22,"show_name":"The Walking Dead","show_type":"Series","show_date":"Sun, Mar 19 2017","orig_date":"2017-03-19","season":"7","ep_val":"14","episode":"Season 7, Episode 14","watched":false}]

I am using this data to populate a select class using ng-options:

ng-options='show.show_name for show in filteredShows track by show.id'

But there are more than one 'The Walking Dead' episode listed therefore it lists 'The Walking Dead' on the dropdown 4 times. How do I go about removing duplicates in this array?

J.Do
  • 303
  • 6
  • 26

4 Answers4

0

You can try this instead:

ng-options='show.show_name for show in filteredShows track by show.show_name'

or you can try _.uniq() method of underscore.js

kaushlendras
  • 220
  • 1
  • 7
0

Why not simply construct a new array in scope that only includes the relevant data points... You can do:

const tmp = {} //Tmp var to store show_name's
$scope.showsOnly = $scope.filteredShows.filter(s => 
    tmp[s.show_name]?0:(tmp[s.show_name] = 1)
)

This filters all show by name (and only allows one instance of a show_name).

AP.
  • 8,082
  • 2
  • 24
  • 33
0

if you are using lodash you can do this with _.uniqBy():

_.uniqBy(data ,"show_name") 

for more info :https://lodash.com/docs/4.17.4#uniqBy

Younes Ouchala
  • 300
  • 1
  • 4
  • 15
0

I think you can try groupBy filter in ng-repeat.

ng-options="show.show_name for show in filteredShows track by show.id | groupBy: 'show_name'"
Marco Cavanna
  • 329
  • 1
  • 5
  • 14