How to create an array containing 1…N

后端 未结 30 1472
旧时难觅i
旧时难觅i 2020-11-22 01:04

I\'m looking for any alternatives to the below for creating a JavaScript array containing 1 through to N where N is only known at runt

30条回答
  •  北海茫月
    2020-11-22 01:45

    Array(8).fill(0).map(Number.call, Number)
    

    Stealing Igors Number.call trick but using fill() to shorten slightly. Only works with ES6 and above.

提交回复
热议问题