Sometime we want to fill an array with a range of numbers like 1 to 10. Let’s how can we do that in JavaScript.
1 | Array.from({ length: 10 }).map((_, i) => ++i); |
In the above snippet, we are first creating an array from an iterable with length of 10 (our range). Then we map to a new value that is being generated by increamenting the index value.
Note: An iterable is an object with a length property and indexed elements.