小编典典

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

all

在 PHP 中,您可以…

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

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

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


阅读 89

收藏
2022-02-25

共1个答案

小编典典

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

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));
2022-02-25