在PHP中,您可以...
range(1, 3); // Array(1, 2, 3)
range("A", "C"); // Array("A", "B", "C")
也就是说,有一个函数可以让您通过传递上限和下限来获取一系列数字或字符。
JavaScript本身有什么内置的功能吗?如果不是,我该如何实现它?
发布于 2010-10-09 10:54:10
它适用于字符和数字,通过一个可选的步骤向前或向后移动。
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;
}
如果您喜欢扩充本机类型,则将其分配给Array.range
。
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;
}
console.log(range("A", "Z", 1));
console.log(range("Z", "A", 1));
console.log(range("A", "Z", 3));
console.log(range(0, 25, 1));
console.log(range(0, 25, 5));
console.log(range(20, 5, 5));
发布于 2015-04-10 18:47:32
对于号码,您可以使用ES6 Array.from()
、which works in everything these days,IE除外:
较短的版本:
Array.from({length: 20}, (x, i) => i);
更长的版本:
Array.from(new Array(20), (x, i) => i);
它创建了一个从0到19 (包括0到19)的数组。这可以进一步缩写为以下形式之一:
Array.from(Array(20).keys());
// or
[...Array(20).keys()];
也可以指定下限和上限,例如:
Array.from(new Array(20), (x, i) => i + *lowerBound*);
更详细地描述这一点的一篇文章:http://www.2ality.com/2014/05/es6-array-methods.html
发布于 2013-10-22 06:51:08
这是我的2分钱:
function range(start, end) {
return Array.apply(0, Array(end - 1))
.map((element, index) => index + start);
}
https://stackoverflow.com/questions/3895478
复制相似问题