Is there a way to generate sequence of characters or numbers in javascript?
For example, I want to create array that contains eight 1s. I can do it with for loop, bu
Without a for loop, here is a solution:
Array.apply(0, Array(8)).map(function() { return 1; })
The explanation follows.
Array(8)
produces a sparse array with 8 elements, all undefined
. The apply
trick will turn it into a dense array. Finally, with map
, we replace that undefined
the (same) value of 1
.