在PHP中,您可以。。。
range(1, 3); // Array(1, 2, 3)
range("A", "C"); // Array("A", "B", "C")
也就是说,有一个函数可以通过传递上下限来获得一系列数字或字符。
JavaScript本机是否有内置的功能?如果没有,我将如何实施?
在PHP中,您可以。。。
range(1, 3); // Array(1, 2, 3)
range("A", "C"); // Array("A", "B", "C")
也就是说,有一个函数可以通过传递上下限来获得一系列数字或字符。
JavaScript本机是否有内置的功能?如果没有,我将如何实施?
当前回答
Array.from(Array((m - n + 1)), (v, i) => n + i); // m > n and both of them are integers.
其他回答
在Vue中循环0和长度之间的数字范围:
<div v-for="index in range" />
computed: {
range () {
let x = [];
for (let i = 0; i < this.myLength; i++)
{
x.push(i);
}
return x;
}
}
也可以使用生成器生成序列。不同之处在于序列中的每个值都是延迟加载的。spread运算符和for为结果工作。星号符号使函数成为生成器。
const range = function*(from,to) {
for(let i = from; i <= to; i++) yield I;
};
[...range(3,5)]// => [3, 4, 5]
https://stackoverflow.com/a/49577331/8784402
带增量/步长
smallest and one-liner[...Array(N)].map((_, i) => from + i * step);
示例和其他备选方案
[...Array(10)].map((_, i) => 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
Array.from(Array(10)).map((_, i) => 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
Array.from(Array(10).keys()).map(i => 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
[...Array(10).keys()].map(i => 4 + i * -2);
//=> [4, 2, 0, -2, -4, -6, -8, -10, -12, -14]
Array(10).fill(0).map((_, i) => 4 + i * 2);
//=> [4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
Array(10).fill().map((_, i) => 4 + i * -2);
//=> [4, 2, 0, -2, -4, -6, -8, -10, -12, -14]
Range Function
const range = (from, to, step) =>
[...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step);
range(0, 9, 2);
//=> [0, 2, 4, 6, 8]
// can also assign range function as static method in Array class (but not recommended )
Array.range = (from, to, step) =>
[...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step);
Array.range(2, 10, 2);
//=> [2, 4, 6, 8, 10]
Array.range(0, 10, 1);
//=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Array.range(2, 10, -1);
//=> []
Array.range(3, 0, -1);
//=> [3, 2, 1, 0]
As Iterators
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)]; // Five Elements
//=> [0, 1, 2, 3, 4]
[...new Range(5, 2)]; // Five Elements With Step 2
//=> [0, 2, 4, 6, 8]
[...new Range(5, -2, 10)]; // Five Elements With Step -2 From 10
//=>[10, 8, 6, 4, 2]
[...new Range(5, -2, -10)]; // Five Elements With Step -2 From -10
//=> [-10, -12, -14, -16, -18]
// Also works with for..of loop
for (i of new Range(5, -2, 10)) console.log(i);
// 10 8 6 4 2
As Generators Only
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));
//=> [-10, -12, -14, -16, -18]
[...Range(5, -2, -10)]; // Five Elements With Step -2 From -10
//=> [-10, -12, -14, -16, -18]
// Also works with for..of loop
for (i of Range(5, -2, 10)) console.log(i);
// 10 8 6 4 2
// Lazy loaded way
const number0toInf = Range(Infinity);
number0toInf.next().value;
//=> 0
number0toInf.next().value;
//=> 1
// ...
带步长/增量的从到
using iteratorsclass 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)]; // First 5 Whole Numbers
//=> [0, 1, 2, 3, 4, 5]
[...new Range2(5, 2)]; // From 0 to 5 with step 2
//=> [0, 2, 4]
[...new Range2(5, -2, 10)]; // From 10 to 5 with step -2
//=> [10, 8, 6]
using Generators
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)]; // From 10 to 5 with step -2
//=> [10, 8, 6]
let even4to10 = Range2(10, 2, 4);
even4to10.next().value;
//=> 4
even4to10.next().value;
//=> 6
even4to10.next().value;
//=> 8
even4to10.next().value;
//=> 10
even4to10.next().value;
//=> undefined
对于字体
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
用一行代码生成字符列表
constcharList=(a,z,d=1)=>(a=a.charCodeAt(),z=z.charCodeAt(),[…数组(Math.floor((z-a)/d)+1)].map((_,i)=>String.fromCharCode(a+i*d)));console.log(“从A到G”,charList('A','G'));console.log(“从A到Z,步长/增量为2”,charList('A','Z',2));console.log(“从Z到P的反向顺序”,charList('Z','P',-1));console.log(“从0到5”,charList(“0”,“5”,1));console.log(“从9到5”,charList('9','5',-1));console.log(“从0到8,步骤2”,charList('0','8',2));console.log(“从α到ω”,charList(“α”,“ω”));console.log(“印地语字符来自क 到ह“,charList('क', 'ह'));console.log(“从А到Е的俄语字符”,charList(“А”,“Е”));
For TypeScriptconst 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)
);
};
方便的函数来完成这个技巧,请运行下面的代码片段
功能范围(开始、结束、步长、偏移){var len=(数学.abs(结束-开始)+((偏移量||0)*2))/(步骤||1)+1;var方向=开始<结束?1 : -1;var startingPoint=开始-(方向*(偏移||0));var stepSize=方向*(步骤||1);return Array(len).fill(0).map(函数(_,索引){return startingPoint+(stepSize*索引);});}console.log('范围(1,5)=>'+范围(1、5));console.log('范围(5,1)=>'+范围(5、1));console.log('范围(5,5)=>'+范围(5、5));console.log('范围(-5,5)=>'+范围(-5、5));console.log('范围(-10,5,5)=>'+范围(-10、5,5));console.log('范围(1,5,1,2)=>'+范围(1、5、1,2;
下面是如何使用它
范围(开始,结束,步长=1,偏移=0);
包括-前进档(5,10)//[5,6,7,8,9,10]包括-向后范围(10,5)//[10,9,8,7,6,5]后退范围(10,2,2)//[10,8,6,4,2]排他-前进范围(5,10,0,-1)//[6,7,8,9]而不是5,10本身偏移-扩展范围(5,10,0,1)//[4,5,6,7,8,9,10,11]偏移-收缩范围(5,10,0,-2)//[7,8]步进-扩展范围(10,0,2,2)//[12,10,8,6,4,2,0,-2]
希望你觉得它有用。
这就是它的工作原理。
基本上,我首先计算得到的数组的长度,并创建一个长度为零的填充数组,然后用所需的值填充
(step | |1)=>其他类似的方法使用step的值,如果没有提供,则使用1我们首先使用(Math.abs(end-start)+((offset | |0)*2))/(step | |1)+1)计算结果数组的长度,以使其更简单(两个方向上的差值*offset/step)获得长度后,我们使用newArray(length).fill(0)创建一个带有初始化值的空数组;在此处检查现在我们有一个数组[0,0,0,..],其长度是我们想要的。我们对其进行映射,并使用array.map(function(){})返回一个具有所需值的新数组var方向=开始<结束?1 : 0; 显然,如果起点不小于终点,我们就需要后退。我的意思是从0到5,反之亦然在每次迭代中,startingPoint+stepSize*索引将为我们提供所需的值
我最喜欢的是生成器generateRange,它带有另一个运行生成器的函数getRange。与许多其他解决方案相比,这样做的一个优点是不需要的阵列不会多次创建。
function* generateRange(start, end, step = 1) {
let current = start;
while (start < end ? current <= end : current >= end) {
yield current;
current = start < end ? current + step : current - step;
}
}
function getRange(start, end, step = 1) {
return [...generateRange(start, end, step)];
}
console.log(getRange(0, 5)) // [ 0, 1, 2, 3, 4, 5 ]
console.log(getRange(10, 0, 2)) // [ 10, 8, 6, 4, 2, 0 ]