To create a new array from 0 to 10, you can use the spread operator (...) along with the Array.from() method in ES6 as follows:
index.tsx110 chars5 lines
This code creates an array of length 11
using the Array
constructor and keys()
method. The keys()
method returns an iterator that generates the integer keys (indexes) in the range of 0
to 10
.
The spread operator ...
is then used to expand the contents of this iterator into a new array and assign it to the newArray
variable. Finally, the contents of newArray
are logged to the console.
Note that the keys()
method doesn't work in all browsers, so you may need to use a polyfill or another approach if you need to support older browsers.
gistlibby LogSnag