https://stackoverflow.com/a/49577331/8784402
With Delta/Step
[...Array(N)].map((_, i) => from + i * step);
Examples and other alternatives
[...Array(10)].map((_, i) => 4 + i * 2);
Array.from(Array(10)).map((_, i) => 4 + i * 2);
Array.from(Array(10).keys()).map(i => 4 + i * 2);
[...Array(10).keys()].map(i => 4 + i * -2);
Array(10).fill(0).map((_, i) => 4 + i * 2);
Array(10).fill().map((_, i) => 4 + i * -2);
const range = (from, to, step) =>
[...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step);
range(0, 9, 2);
Array.range = (from, to, step) =>
[...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step);
Array.range(2, 10, 2);
Array.range(0, 10, 1);
Array.range(2, 10, -1);
Array.range(3, 0, -1);
class Range {
constructor(total = 0, step = 1, from = 0) {
this[Symbol.iterator] = function* () {
for (let i = 0; i < total; yield from + i++ * step) {}
};
}
}
[...new Range(5)];
[...new Range(5, 2)];
[...new Range(5, -2, 10)];
[...new Range(5, -2, -10)];
for (i of new Range(5, -2, 10)) console.log(i);
const Range = function* (total = 0, step = 1, from = 0) {
for (let i = 0; i < total; yield from + i++ * step) {}
};
Array.from(Range(5, -2, -10));
[...Range(5, -2, -10)];
for (i of Range(5, -2, 10)) console.log(i);
const number0toInf = Range(Infinity);
number0toInf.next().value;
number0toInf.next().value;
From-To with steps/delta
class Range2 {
constructor(to = 0, step = 1, from = 0) {
this[Symbol.iterator] = function* () {
let i = 0,
length = Math.floor((to - from) / step) + 1;
while (i < length) yield from + i++ * step;
};
}
}
[...new Range2(5)];
[...new Range2(5, 2)];
[...new Range2(5, -2, 10)];
const Range2 = function* (to = 0, step = 1, from = 0) {
let i = 0,
length = Math.floor((to - from) / step) + 1;
while (i < length) yield from + i++ * step;
};
[...Range2(5, -2, 10)];
let even4to10 = Range2(10, 2, 4);
even4to10.next().value;
even4to10.next().value;
even4to10.next().value;
even4to10.next().value;
even4to10.next().value;
For Typescript
class _Array<T> extends Array<T> {
static range(from: number, to: number, step: number): number[] {
return Array.from(Array(Math.floor((to - from) / step) + 1)).map(
(v, k) => from + k * step
);
}
}
_Array.range(0, 9, 1);
https://stackoverflow.com/a/64599169/8784402
Generate Character List with one-liner
const charList = (a,z,d=1)=>(a=a.charCodeAt(),z=z.charCodeAt(),[...Array(Math.floor((z-a)/d)+1)].map((_,i)=>String.fromCharCode(a+i*d)));
console.log("from A to G", charList('A', 'G'));
console.log("from A to Z with step/delta of 2", charList('A', 'Z', 2));
console.log("reverse order from Z to P", charList('Z', 'P', -1));
console.log("from 0 to 5", charList('0', '5', 1));
console.log("from 9 to 5", charList('9', '5', -1));
console.log("from 0 to 8 with step 2", charList('0', '8', 2));
console.log("from α to ω", charList('α', 'ω'));
console.log("Hindi characters from क to ह", charList('क', 'ह'));
console.log("Russian characters from А to Я", charList('А', 'Я'));
const charList = (p: string, q: string, d = 1) => {
const a = p.charCodeAt(0),
z = q.charCodeAt(0);
return [...Array(Math.floor((z - a) / d) + 1)].map((_, i) =>
String.fromCharCode(a + i * d)
);
};
$R
function, but other than that I don't really think so. – Yi JiangArray.from("ABC") //['A', 'B', 'C']
This is the closest thing I can find for the second part of the question. – Andrew_1510Array.apply(null, { length: 10 }).map(eval.call, Number)
– csharpfolkconst range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));
(see 'Sequence generator (range)' from MSDN) NOTE: This function only works if all parameters are specified (ie.range(1,5,1)
produces the array[1,2,3,4,5]
, butrange(1,5)
produces an empty array) – Dave F