JavaScript function similar to Python range()

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

    Here's a small extension for one of the answers in case you need to specify both starting and ending position of the range:

    let range = (start, end) => Array.from(Array(end + 1).keys()).slice(start);
    

提交回复
热议问题