JavaScript 是否有类似“range()”的方法来在提供的范围内生成范围?

新手上路,请多包涵

在 PHP 中,您可以…

range(1, 3); // Array(1, 2, 3)
range("A", "C"); // Array("A", "B", "C")

也就是说,有一个函数可以让您通过传递上限和下限来获取一系列数字或字符。

是否有任何内置于 JavaScript 的本机功能?如果没有,我将如何实施?

原文由 alex 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 720
2 个回答

它适用于字符和数字,可以通过可选步骤向前或向后移动。

 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;

 }

js小提琴

如果增加本机类型是您的事,那么将其分配给 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));

原文由 alex 发布,翻译遵循 CC BY-SA 3.0 许可协议

数字

[...Array(5).keys()];
 => [0, 1, 2, 3, 4]

字符迭代

String.fromCharCode(...[...Array('D'.charCodeAt(0) - 'A'.charCodeAt(0) + 1).keys()].map(i => i + 'A'.charCodeAt(0)));
 => "ABCD"

迭代

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"

作为函数

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)))
}

作为类型函数

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() 函数

_.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"

没有库的旧非 es6 浏览器:

 Array.apply(null, Array(5)).map(function (_, i) {return i;});
 => [0, 1, 2, 3, 4]

 console.log([...Array(5).keys()]);

(ES6 归功于 nils petersohn 和其他评论者)

原文由 Fuji 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题