2

I've read a documentation, I've tried to google it and check with code but still I can't find an answer.

I have an array of string, for example ['Tom', 'Sarah', 'Ben']. Its content will change dynamically and regularly. I'd like to use a trackBy function with it.

  1. Generally, is there sense to do it?
  2. If so, how should I use trackBy with such array? I want to avoid converting data to object with id.

Thank you

Kiran Mistry
  • 2,614
  • 3
  • 12
  • 28
Roghul
  • 31
  • 1
  • Hi @Roghul, I suggest you [use `trackBy` with primitive arrays to avoid sorting issues](https://stackoverflow.com/questions/40863074/ngfor-behaviour-on-primitive-data-type/40863118#40863118). – Rafael Neto Dec 24 '21 at 11:13

1 Answers1

1

A typical way is to use index.

html

<div *ngFor="let e of array; trackBy: trackByFunc;">
   ...
</div>

ts

trackByFunc = (index: number, value: string) => index;
N.F.
  • 3,844
  • 3
  • 22
  • 53