0

I have a list of data from a json with a drop down filter. The list is grouped by with its name. But some data is repeating. I don"t want to list repeated data. How to do that. Please see the below link. Please help me.

http://plnkr.co/edit/zFUkWkYde9gpmeKZ9fP1?p=preview

angular.forEach($scope.locationList, function(item){
var locations = $scope.loacationByCategory[item.field_location_category[0]];
if(locations){
  locations.push(item);
}else{
  $scope.loacationByCategory[item.field_location_category[0]] = [item];
  $scope.categories.push(item.field_location_category[0]); 
}
});

Thank you.

Vimal
  • 2,767
  • 3
  • 19
  • 24
  • 1
    possible duplicate of [Angular JS - How to make ng-repeat filter out duplicate results](http://stackoverflow.com/questions/15914658/angular-js-how-to-make-ng-repeat-filter-out-duplicate-results) – sloth Jan 20 '15 at 13:18
  • 1
    possible dupicate of [AngularJs Remove duplicate elements in ng-repeat](https://stackoverflow.com/questions/20222555/angularjs-remove-duplicate-elements-in-ng-repeat?lq=1) – sloth Jan 20 '15 at 13:20
  • if you don't need multiple names then y did u add it. here tid is unique i guess. each item has different tid. so if want to remove multiple items, avoid diffrent tid and same name. use same tid and same name – simon Jan 20 '15 at 13:37

1 Answers1

2

You could use Underscore or lodash to do:

var uniqueLocations = _.uniq($scope.locationList, 'name');

This will make sure that for each name property, only one unique value exists. Updated plunkr: http://plnkr.co/edit/aEGPAPIRIqxLFyWb956d?p=preview

Note that this will remove duplicates, even if they are in a different category. You could write a custom callback function for _.uniq() (docs) that de-duplicates within categories, or do your filtering later in your processing logic.

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
  • 1
    The bigger question is, why are there duplicate items in your JSON? They have the same `name` but a different `tid`. This usually indicates they are different items. This may be something you should change on the database or architectural level. – Nate Barbettini Jan 20 '15 at 13:36