How to generate sequence of numbers/chars in javascript?

前端 未结 18 1185
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 06:22

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

18条回答
  •  粉色の甜心
    2020-12-13 06:44

    2016 - Modern Browser functionality has arrived. No need for jquery all the time.

    Array.from({length: 8}, (el, index) => 1 /* or index */);
    

    You can substitute the arrow function with a simple callback function to reach a slightly wider range of supported browsers. It's, for me at least, the easiest way to iterate over an initialized array in one step.

    Note: IE is not supported in this solution, but there is a polyfill for that at developer.mozilla.org/...

提交回复
热议问题