0

I'm using https://www.npmjs.com/package/uniqid to generate random IDs. I would want to generate a list of for example 20 ids so I tried something like this :

 console.log(Array(20).fill(uniqid.time()));

The problem is that the generated list contain the same generated id and not differents ids. Is there any way to create a new id for each element ?

AlyaKra
  • 486
  • 8
  • 22
  • 1
    fill takes one value and use it, you can call map instead to call time fn for each el `console.log(Array(20).fill(0).map(x => uniqueId.time()));` – udalmik Feb 18 '22 at 16:49
  • fill just fills, see [this question](https://stackoverflow.com/questions/5836833/create-an-array-with-random-values) and replace *random value* with your function call there is various ways to do it – Lawrence Cherone Feb 18 '22 at 16:49
  • 1
    Fill replaces each element with what you pass in. You should have used `Array.from({ length: 20 }).map(() => uniqid.time())` which would make a separate call for each element. – samthecodingman Feb 18 '22 at 16:49
  • Thank you guys, I didn't think of mapping though the array, it actually worked ! – AlyaKra Feb 18 '22 at 16:51

0 Answers0