If you want an array holding the numbers 1 through 10, you can use the spread operator, like this:
JavaScript:
[...Array(10).keys()].map(e => e + 1)
First,
Array(10) creates an empty array of size 10.
Then,
.keys() gives you an iterator of the array's keys (that's 0 through 9, used to access the items of the array created).
The spread operator
... is only valid inside round brackets (for function call), square brackets (for creating arrays) or curly brackets (for cloning objects), and only works on iterable objects (like strings, arrays or iterators). It's basically the equivalent to doing
[ A[0], A[1], A[2], ..., A[9] ] if
A was an array of size 10.
Since we've now created an array that contains 0 to 9, but we want 1 to 10, we use
.map(e => e + 1). The
map(f) function iterates through the entire array, passes each element of that array into function
f and creates a new array with the same size, where each element is the result of function
f.
Finally,
e => e + 1 is an arrow function that takes an argument
e (in this case a number from 0 to 9), and returns
e + 1 (giving us 1 to 10). It's similar to
function(e) { return e + 1 }, but shorter. Arrow functions also behave differently regarding their context, which affects
this, but isn't really important in this case, since we don't use
this.