Does JavaScript have a method like “range()” to generate a range within the supplied bounds?

后端 未结 30 2760
广开言路
广开言路 2020-11-22 00:51

In PHP, you can do...

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

That is, there is a function that l

相关标签:
30条回答
  • 2020-11-22 01:49

    Though this is not from PHP, but an imitation of range from Python.

    function range(start, end) {
        var total = [];
    
        if (!end) {
            end = start;
            start = 0;
        }
    
        for (var i = start; i < end; i += 1) {
            total.push(i);
        }
    
        return total;
    }
    
    console.log(range(10)); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
    console.log(range(0, 10)); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    console.log(range(5, 10)); // [5, 6, 7, 8, 9] 
    
    0 讨论(0)
  • 2020-11-22 01:49

    This one works also in reverse.

    const range = ( a , b ) => Array.from( new Array( b > a ? b - a : a - b ), ( x, i ) => b > a ? i + a : a - i );
    
    range( -3, 2 ); // [ -3, -2, -1, 0, 1 ]
    range( 1, -4 ); // [ 1, 0, -1, -2, -3 ]
    
    0 讨论(0)
  • 2020-11-22 01:49

    You can use following one-liner to keep things short and simple

    var start = 4;
    var end = 20;
    console.log(Array(end - start + 1).fill(start).map((x, y) => x + y));

    0 讨论(0)
  • 2020-11-22 01:50

    Simple range function:

    function range(start, stop, step) {
        var a = [start], b = start;
        while (b < stop) {
            a.push(b += step || 1);
        }
        return a;
    }
    

    To incorporate the BitInt data type some check can be included, ensuring that all variables are same typeof start:

    function range(start, stop, step) {
        var a = [start], b = start;
        if (typeof start == 'bigint') {
            stop = BigInt(stop)
            step = step? BigInt(step): 1n;
        } else
            step = step || 1;
        while (b < stop) {
            a.push(b += step);
        }
        return a;
    }
    

    To remove values higher than defined by stop e.g. range(0,5,2) will include 6, which shouldn't be.

    function range(start, stop, step) {
        var a = [start], b = start;
        while (b < stop) {
            a.push(b += step || 1);
        }
        return (b > stop) ? a.slice(0,-1) : a;
    }
    
    0 讨论(0)
  • 2020-11-22 01:54

    As far as generating a numeric array for a given range, I use this:

    function range(start, stop)
    {
        var array = [];
    
        var length = stop - start; 
    
        for (var i = 0; i <= length; i++) { 
            array[i] = start;
            start++;
        }
    
        return array;
    }
    
    console.log(range(1, 7));  // [1,2,3,4,5,6,7]
    console.log(range(5, 10)); // [5,6,7,8,9,10]
    console.log(range(-2, 3)); // [-2,-1,0,1,2,3]
    

    Obviously, it won't work for alphabetical arrays.

    0 讨论(0)
  • 2020-11-22 01:55

    My new favorite form (ES2015)

    Array(10).fill(1).map((x, y) => x + y)
    

    And if you need a function with a step param:

    const range = (start, stop, step = 1) =>
      Array(Math.ceil((stop - start) / step)).fill(start).map((x, y) => x + y * step)
    
    0 讨论(0)
提交回复
热议问题