Does JavaScript have a method like "range()" to generate a range within the supplied bounds?
Numbers
[...Array(5).keys()]; => [0, 1, 2, 3, 4]
Character iteration
String.fromCharCode(...[...Array('D'.charCodeAt(0) - 'A'.charCodeAt(0) + 1).keys()].map(i => i + 'A'.charCodeAt(0))); => "ABCD"
Iteration
for (const x of Array(5).keys()) { console.log(x, String.fromCharCode('A'.charCodeAt(0) + x));} => 0,"A" 1,"B" 2,"C" 3,"D" 4,"E"
As functions
function range(size, startAt = 0) { return [...Array(size).keys()].map(i => i + startAt);}function characterRange(startChar, endChar) { return String.fromCharCode(...range(endChar.charCodeAt(0) - startChar.charCodeAt(0), startChar.charCodeAt(0)))}
As typed functions
function range(size:number, startAt:number = 0):ReadonlyArray<number> { return [...Array(size).keys()].map(i => i + startAt);}function characterRange(startChar:string, endChar:string):ReadonlyArray<string> { return String.fromCharCode(...range(endChar.charCodeAt(0) - startChar.charCodeAt(0), startChar.charCodeAt(0)))}
lodash.js _.range()
function
_.range(10); => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]_.range(1, 11); => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]_.range(0, 30, 5); => [0, 5, 10, 15, 20, 25]_.range(0, -10, -1); => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]String.fromCharCode(..._.range('A'.charCodeAt(0), 'D'.charCodeAt(0) + 1)); => "ABCD"
Old non es6 browsers without a library:
Array.apply(null, Array(5)).map(function (_, i) {return i;}); => [0, 1, 2, 3, 4]
console.log([...Array(5).keys()]);
(ES6 credit to nils petersohn and other commenters)
For numbers you can use ES6 Array.from()
, which works in everything these days except IE:
Shorter version:
Array.from({length: 20}, (x, i) => i);
Longer version:
Array.from(new Array(20), (x, i) => i);
which creates an array from 0 to 19 inclusive. This can be further shortened to one of these forms:
Array.from(Array(20).keys());// or[...Array(20).keys()];
Lower and upper bounds can be specified too, for example:
Array.from(new Array(20), (x, i) => i + *lowerBound*);
An article describing this in more detail: http://www.2ality.com/2014/05/es6-array-methods.html
My new favorite form (ES2015)
Array(10).fill(1).map((x, y) => x + y)
And if you need a function with a step
param:
const range = (start, stop, step = 1) => Array(Math.ceil((stop - start) / step)).fill(start).map((x, y) => x + y * step)