Does JavaScript have a method like “range()” to generate a range within the supplied bounds?

后端 未结 30 2768
广开言路
广开言路 2020-11-22 00:51

In PHP, you can do...

range(1, 3); // Array(1, 2, 3)
range(\"A\", \"C\"); // Array(\"A\", \"B\", \"C\")

That is, there is a function that l

30条回答
  •  无人及你
    2020-11-22 01:56

    My codegolfing coworker came up with this (ES6), inclusive:

    (s,f)=>[...Array(f-s+1)].map((e,i)=>i+s)
    

    non inclusive:

    (s,f)=>[...Array(f-s)].map((e,i)=>i+s)
    

提交回复
热议问题