In JavaScript, when creating an array of a certain length, you can use Array.from
to create an array of the desired length:
Array.from({ length: 5 }, (_, x) => x)
// => [ 0, 1, 2, 3, 4 ]
This is similar to this older idiom:
Array(5).fill().map((_, x) => x)
However, TypeScript will complain about the missing argument to fill
, and you have to put in a dummy value.
Array(5).fill(0).map((_, x) => x)
However, it may confuse the reader (e.g. “what does 0
mean here?”), so now I prefer the Array.from
version.