JavaScript function similar to Python range()

前端 未结 24 1591
南旧
南旧 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:48

    Here's how i do it

    let n = 5 
    [...Array(n).keys()].map(x=>{console.log(x)})
    

    output

    0
    1
    2
    3
    4
    

提交回复
热议问题