JavaScript function similar to Python range()

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

    This is my preferred way. It allows you to specify one or two inputs like in Python.

    function range(start, end) {
      return Array.from(Array(end||start).keys()).slice(!!end*start)
    }
    

提交回复
热议问题