2017-04-06 62 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过滤器。

​​