0

I'm having some issues using ng-repeat with track by because sometimes duplicate data comes in (same user) because the user was pushed down in the date ordered list (API side) after a newer entry has been pushed to the beginning of the array.

I wonder if it's possible to do something like (pseudo code)

try {
     render(element)
} catch (DuplicateElementException $e) {
     // ignore element
     return;
}
  • remove duplicate before sending it to the scope, for performance – Nickydonna Feb 02 '17 at 13:47
  • What are you tracking the array by, and what error are you encountering? – Harris Feb 02 '17 at 14:08
  • I am tracking the array by user.id and getting a duplicate element error. @donnanicolas would that not set off the performance gains of track by as each addition of a batch of users in the users list would require 20 loops (assuming 20 users are added) through all of the current users in the list? – Edgaras Kazimieras Kazlauskas Feb 05 '17 at 11:35
  • Everything you put on the scope is saved for latter comparison, the bigger the array is, more memory is needed. Filter before attaching to angular, it's also easier than filtering in an ngFor – Nickydonna Feb 05 '17 at 15:31
  • some thing like that would help? user in users track by $id($index) – MMK Feb 07 '17 at 16:33

1 Answers1

0

The best solution is to transform your array before you attach it to the DOM. This is common behaviour in front end application to transform the data received by the backend before displaying it.

Just loop through your data and remove the duplicate and generate the array you NEED for the HTML output

Tonio
  • 4,082
  • 4
  • 35
  • 60