在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本机是否有内置的功能?如果没有,我将如何实施?
当前回答
它适用于字符和数字,通过可选步骤向前或向后移动。
var range = function(start, end, step) {
var range = [];
var typeofStart = typeof start;
var typeofEnd = typeof end;
if (step === 0) {
throw TypeError("Step cannot be zero.");
}
if (typeofStart == "undefined" || typeofEnd == "undefined") {
throw TypeError("Must pass start and end arguments.");
} else if (typeofStart != typeofEnd) {
throw TypeError("Start and end arguments must be of same type.");
}
typeof step == "undefined" && (step = 1);
if (end < start) {
step = -step;
}
if (typeofStart == "number") {
while (step > 0 ? end >= start : end <= start) {
range.push(start);
start += step;
}
} else if (typeofStart == "string") {
if (start.length != 1 || end.length != 1) {
throw TypeError("Only strings with one character are supported.");
}
start = start.charCodeAt(0);
end = end.charCodeAt(0);
while (step > 0 ? end >= start : end <= start) {
range.push(String.fromCharCode(start));
start += step;
}
} else {
throw TypeError("Only string and number types are supported");
}
return range;
}
jsFiddle。
如果扩充本机类型是您的事情,那么将其分配给Array.range。
var范围=函数(开始、结束、步骤){var范围=[];var typeofStart=启动类型;var typeofEnd=结束类型;如果(步骤==0){throw TypeError(“步长不能为零。”);}if(类型开始==“undefined”| |类型结束==“未定义”){throw TypeError(“必须传递开始和结束参数。”);}否则如果(typeofStart!=typeofEnd){throw TypeError(“开始和结束参数必须是相同的类型。”);}步骤类型==“未定义”&&(步骤=1);if(结束<开始){step=-步骤;}if(开始类型==“number”){while(步骤>0?结束>=开始:结束<=开始){范围.推(启动);开始+=步骤;}}否则if(typeofStart==“string”){如果(start.length!=1 | | end.length;=1){throw TypeError(“仅支持带有一个字符的字符串。”);}start=start.charCodeAt(0);end=end.charCodeAt(0);while(步骤>0?结束>=开始:结束<=开始){range.push(String.fromCharCode(开始));开始+=步骤;}}其他{throw TypeError(“仅支持字符串和数字类型”);}返回范围;}console.log(范围(“A”,“Z”,1));console.log(范围(“Z”,“A”,1));console.log(范围(“A”,“Z”,3));console.log(范围(0,25,1));console.log(范围(0,25,5));console.log(范围(20,5,5));
其他回答
// range() 0..10, step=1
// range(max) 0..max, step=1
// range(min,max) min..max, step=1
// range(min,step,max) min..max, step=step
// Use:
// console.log(...range(3));
// Array.from(range(5))
// [...range(100)]
// for (const v of range(1,10)) { ...
function* range(...args) {
let [min, step, max] = {
0: [0, 1, 10],
1: [0, args[0] >= 0 ? 1 : -1, args[0]],
2: [args[0], args[1] >= args[0] ? 1 : -1, args[1]],
3: args,
}[args.length] || [];
if (min === undefined) throw new SyntaxError("Too many arguments");
let x = min;
while (step >= 0 ? x < max : x > max) {
yield x;
x += step
}
}
console.log(...range()); // 0 1 2 3 4 5 6 7 8 9
console.log(...range(3)); // 0 1 2
console.log(...range(2, 5)); // 2 3 4
console.log(...range(5, 2)); // 5 4 3
console.log(...range(3, -3)); // 3 2 1 0 -1 -2
console.log(...range(-3, 3)); // -3 -2 -1 0 1 2
console.log(...range(-5, -2));// -5 -4 -3
console.log(...range(-2, -5));// -2 -3 -4
我喜欢下面的路
var范围=函数(x,y){return数组(y-x+1).fill(x).map((a,b)=>{return a+b}).filter(i=>i>=x);};console.log(范围(3,10));
您可以使用lodash函数.range(10)https://lodash.com/docs#range
使用范围([start,]stop[,step])签名完成ES6实现:
function range(start, stop, step=1){
if(!stop){stop=start;start=0;}
return Array.from(new Array(int((stop-start)/step)), (x,i) => start+ i*step)
}
如果要自动负步进,请添加
if(stop<start)step=-Math.abs(step)
或者更简单地说:
range=(b, e, step=1)=>{
if(!e){e=b;b=0}
return Array.from(new Array(int((e-b)/step)), (_,i) => b<e? b+i*step : b-i*step)
}
如果你有巨大的射程,看看保罗·莫雷蒂的发电机方法
对一些不同的范围函数进行了研究。检查执行这些函数的不同方法的jsperf比较。当然不是一个完美或详尽的列表,但应该有帮助:)
获胜者是。。。
function range(lowEnd,highEnd){
var arr = [],
c = highEnd - lowEnd + 1;
while ( c-- ) {
arr[c] = highEnd--
}
return arr;
}
range(0,31);
从技术上讲,它不是firefox上最快的,但铬合金上的疯狂速度差(imho)弥补了这一点。
还有一个有趣的观察是,与firefox相比,chrome使用这些阵列功能的速度要快得多。Chrome速度至少快4或5倍。