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

后端 未结 30 2795
广开言路
广开言路 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:30

    Numbers

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

    Character iteration

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

    Iteration

    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"
    

    As functions

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

    As typed functions

    function range(size:number, startAt:number = 0):ReadonlyArray {
        return [...Array(size).keys()].map(i => i + startAt);
    }
    
    function characterRange(startChar:string, endChar:string):ReadonlyArray {
        return String.fromCharCode(...range(endChar.charCodeAt(0) -
                startChar.charCodeAt(0), startChar.charCodeAt(0)))
    }
    

    lodash.js _.range() function

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

    Old non es6 browsers without a library:

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

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

    (ES6 credit to nils petersohn and other commenters)

提交回复
热议问题