0

I find my self using _.range a lot, and sometimes I don't want to drag with me lodash just for that reason.

What is the best way to do this without lodash?

My best solution is:

   export function range(n) {
     return new Array(n).fill(0).map((a, i) => i);
   }
starak
  • 18
  • 4
  • 1
    I don't know if this counts as an answer, so I'm posting as a comment, but you can import [`lodash.range`](https://www.npmjs.com/package/lodash.range) as a standalone module without the rest of `lodash`. – Patrick Roberts Feb 20 '18 at 19:19

1 Answers1

4
 return Array.from({length: n}, (_, i) => i);

...saves a few bytes.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151