Ad

Ng-repeat Track By $index Display The Item Only Once If The Similar Key Exists Multiple Times In Angularjs

- 1 answer

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?

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad