How to create an array containing 1…N

后端 未结 30 1953
旧时难觅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:47

    If you happen to be using d3.js in your app as I am, D3 provides a helper function that does this for you.

    So to get an array from 0 to 4, it's as easy as:

    d3.range(5)
    [0, 1, 2, 3, 4]
    

    and to get an array from 1 to 5, as you were requesting:

    d3.range(1, 5+1)
    [1, 2, 3, 4, 5]
    

    Check out this tutorial for more info.

提交回复
热议问题