Search code examples
javascriptangularjsangularjs-ng-repeat

ng-repeat track by $index display the item only once if the similar key exists multiple times in angularjs


I was getting [ngRepeat:dupes]: error in my angularjs code. So I searched on stackoverflow and found the solution which stated to use track by $index. This is working for me now. But, it displays the item multiple times if the same key is present multiple times. I want to display only one item if the same key exists multiple times. here is my sample code from the view :

<div ng-repeat="user in users | filter:myFilter track by $index">
</div>

here is the current output : enter image description here

Insted of showing these two cards, I want the code to show only one card as they both are the same. How do I do it?


Solution

  • pass array into a function and create an array without the repeating values like myFunction(users) so the repeat section will become ng-repeat="user in myFunction(users)". Write the function yourself.