javascript - Create Simple Dynamic Array
var arr = [];
for(var i=1; i<=mynumber; i++) {
arr.push(i.toString());
}
With ES2015, this can be achieved concisely in a single expression using the Array.from
method like so:
Array.from({ length: 10 }, (_, idx) => `${++idx}`)
The first argument to from
is an array like object that provides a length property. The second argument is a map function that allows us to replace the default undefined
values with their adjusted index values as you requested. Checkout the specification here