Last active
June 13, 2020 23:04
-
-
Save xgrommx/a25ffa3a7753e01ee679 to your computer and use it in GitHub Desktop.
Generate ranges in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const range1 = len => Object.keys(new Int8Array(len)).map(parseFloat); | |
const range2 = len => Array.apply(null, {length: len}).map(Number.call, Number); | |
const range3 = (start, end) => Array.apply(null, Array(end - start)).map((_, i) => start + i); | |
const range4 = (start, end) => [...Array(end - start)].map((_, i) => start + i); | |
const range5 = len => Object.keys(Array.apply(null, Array(len))); | |
const range6 = (start, end) => Array.from(Array(end - start), (_, i) => start + i); |
ghaiklor
commented
Feb 9, 2016
const range = (start, end) => Array.from({length: end - start}, (_, i) => start + i);
const range8 = n => Array(n).fill().map((v, i) => i)
const range9 = (start, end) => Array(end - start).fill().map((v, i) => i + start)
@shvaikalesh's is most efficient, outside of possibly using a for loop.
@xgrommx looks like range4
and range6
are identical
@yairhaimo getting errors on range8
and range9
[ts] Supplied parameters do not match any signature of call target.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment