JavaScript function similar to Python range()

前端 未结 24 1639
南旧
南旧 2020-11-30 21:17

Is there a function in JavaScript similar to Python\'s range()?

I think there should be a better way than to write the following lines every time:

24条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 21:30

    For getting an array of size x, here's an one-liner without using any library

    var range = n => Array(n + 1).join(1).split('').map((x, i) => i)
    

    works as

    > range(4)
    [0, 1, 2, 3]
    

提交回复
热议问题