2017-04-06 70 views
0

取消複製在$scope.filteredShows的數據是這樣的:在數組對象

[{"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}] 

我使用這個數據使用ng-options來填充select類:

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

但也有不止一個因此列出的「行屍走肉」一集在下拉列表中列出了「行屍走肉」4次。我該如何去除這個數組中的重複項?

+0

請參閱[本](http://stackoverflow.com/a/9229821/3931488)。 –

+0

您是否使用angular-ui? –

+1

[如何使重複過濾出重複結果]可能的重複(http://stackoverflow.com/questions/15914658/how-to-make-ng-repeat-filter-out-duplicate-results) –

回答

0

爲什麼不能簡單地構建範圍的新數組僅包括相關數據點...你可以這樣做:

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) 
) 

這種過濾所有節目的名字(僅允許show_name的一個實例)。更多信息

_.uniqBy(data ,"show_name") 

0

可以代替試試這個:

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

或者你可以嘗試_.uniq()underscore.js的方法

0

我想你可以嘗試在NG-重複groupBy過濾器。

​​