0

I have these column:

Column
   1     
   1         
   2   

What I want in the result is that the duplicated '1' will not be repeated in the ng-repeat. Here's the illustration:

Column  
   1      
   2   

Is there a way to check that if a value is repeated it will not be included in the iteration of the ng-repeat?

Here's my working fiddle so far:

<div ng-app="myApp" ng-controller="myCtrl">
    <table>
        <tbody>
            <tr ng-repeat="item in obj">
                <td> {{ item.col1 }} </td>
            </tr>
        </tbody>
    </table>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function ($scope) {
        $scope.obj = [{ col1: 1}, { col1: 1}, { col1: 2}];
    });
</script>

EDIT: I just want to know if is there any other way aside from using a filter? Since in the duplicate question, all the answers discussed about using filter.

JP Dolocanog
  • 451
  • 3
  • 19
  • you can create a filter that filters out unique values from the object – sahil gupta Nov 08 '17 at 08:57
  • 2
    I would suggest pre-processing the list to remove any duplicates instead of doing it in the UI code. – SQDK Nov 08 '17 at 08:57
  • 1
    see https://stackoverflow.com/questions/20222555/angularjs-remove-duplicate-elements-in-ng-repeat – Meiko Rachimow Nov 08 '17 at 08:57
  • Possible duplicate of [How to make ng-repeat filter out duplicate results](https://stackoverflow.com/questions/15914658/how-to-make-ng-repeat-filter-out-duplicate-results) – Jeremy Thille Nov 08 '17 at 08:58
  • @SQDK can you give me some little ideas to think and work on? – JP Dolocanog Nov 08 '17 at 09:09
  • @JPDolocanog Google "remove duplicates from javascript array" to get some inspiration for the algorithm. After you remove the duplicates, you can set `obj` to be the new list without duplicates. This should give you what you need in a more simple way than writing a filter i think. – SQDK Nov 08 '17 at 10:31

1 Answers1

1

It seems you need to get unique values only. There is a quick solution to use the unique filter in ng-repeat.

AngularJS doesn't include a unique filter by default. You can use the one from angular-filter. Just include the JavaScript.

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.8/angular-filter.min.js"></script>

and include the dependeny in your app,

var app = angular.module('myApp', ['angular.filter']);

then add | unique: filter in ng-repeat.