How to create an array containing 1…N

后端 未结 30 1347
旧时难觅i
旧时难觅i 2020-11-22 01:04

I\'m looking for any alternatives to the below for creating a JavaScript array containing 1 through to N where N is only known at runt

相关标签:
30条回答
  • 2020-11-22 02:02
    for(var i,a=[i=0];i<10;a[i++]=i);
    

    a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    0 讨论(0)
  • 2020-11-22 02:03

    If you are using lodash, you can use _.range:

    _.range([start=0], end, [step=1])

    Creates an array of numbers (positive and/or negative) progressing from start up to, but not including, end. A step of -1 is used if a negative start is specified without an end or step. If end is not specified, it's set to start with start then set to 0.

    Examples:

    _.range(4);
    // ➜ [0, 1, 2, 3]
    
    _.range(-4);
    // ➜ [0, -1, -2, -3]
    
    _.range(1, 5);
    // ➜ [1, 2, 3, 4]
    
    _.range(0, 20, 5);
    // ➜ [0, 5, 10, 15]
    
    _.range(0, -4, -1);
    // ➜ [0, -1, -2, -3]
    
    _.range(1, 4, 0);
    // ➜ [1, 1, 1]
    
    _.range(0);
    // ➜ []
    
    0 讨论(0)
  • 2020-11-22 02:03

    Final Summary report .. Drrruummm Rolll -

    This is the shortest code to generate an Array of size N (here 10) without using ES6. Cocco's version above is close but not the shortest.

    (function(n){for(a=[];n--;a[n]=n+1);return a})(10)
    

    But the undisputed winner of this Code golf(competition to solve a particular problem in the fewest bytes of source code) is Niko Ruotsalainen . Using Array Constructor and ES6 spread operator . (Most of the ES6 syntax is valid typeScript, but following is not. So be judicious while using it)

    [...Array(10).keys()]
    
    0 讨论(0)
  • 2020-11-22 02:04

    In ES6 you can do:

    Array(N).fill().map((e,i)=>i+1);

    http://jsbin.com/molabiluwa/edit?js,console

    Edit: Changed Array(45) to Array(N) since you've updated the question.

    console.log(
      Array(45).fill(0).map((e,i)=>i+1)
    );

    0 讨论(0)
  • 2020-11-22 02:05

    You can do so:

    var N = 10; 
    Array.apply(null, {length: N}).map(Number.call, Number)
    

    result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    or with random values:

    Array.apply(null, {length: N}).map(Function.call, Math.random)
    

    result: [0.7082694901619107, 0.9572225909214467, 0.8586748542729765, 0.8653848143294454, 0.008339877473190427, 0.9911756622605026, 0.8133423360995948, 0.8377588465809822, 0.5577575915958732, 0.16363654541783035]

    Explanation

    First, note that Number.call(undefined, N) is equivalent to Number(N), which just returns N. We'll use that fact later.

    Array.apply(null, [undefined, undefined, undefined]) is equivalent to Array(undefined, undefined, undefined), which produces a three-element array and assigns undefined to each element.

    How can you generalize that to N elements? Consider how Array() works, which goes something like this:

    function Array() {
        if ( arguments.length == 1 &&
             'number' === typeof arguments[0] &&
             arguments[0] >= 0 && arguments &&
             arguments[0] < 1 << 32 ) {
            return [ … ];  // array of length arguments[0], generated by native code
        }
        var a = [];
        for (var i = 0; i < arguments.length; i++) {
            a.push(arguments[i]);
        }
        return a;
    }
    

    Since ECMAScript 5, Function.prototype.apply(thisArg, argsArray) also accepts a duck-typed array-like object as its second parameter. If we invoke Array.apply(null, { length: N }), then it will execute

    function Array() {
        var a = [];
        for (var i = 0; i < /* arguments.length = */ N; i++) {
            a.push(/* arguments[i] = */ undefined);
        }
        return a;
    }
    

    Now we have an N-element array, with each element set to undefined. When we call .map(callback, thisArg) on it, each element will be set to the result of callback.call(thisArg, element, index, array). Therefore, [undefined, undefined, …, undefined].map(Number.call, Number) would map each element to (Number.call).call(Number, undefined, index, array), which is the same as Number.call(undefined, index, array), which, as we observed earlier, evaluates to index. That completes the array whose elements are the same as their index.

    Why go through the trouble of Array.apply(null, {length: N}) instead of just Array(N)? After all, both expressions would result an an N-element array of undefined elements. The difference is that in the former expression, each element is explicitly set to undefined, whereas in the latter, each element was never set. According to the documentation of .map():

    callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

    Therefore, Array(N) is insufficient; Array(N).map(Number.call, Number) would result in an uninitialized array of length N.

    Compatibility

    Since this technique relies on behaviour of Function.prototype.apply() specified in ECMAScript 5, it will not work in pre-ECMAScript 5 browsers such as Chrome 14 and Internet Explorer 9.

    0 讨论(0)
  • 2020-11-22 02:06

    Array(...Array(9)).map((_, i) => i);
    
    console.log(Array(...Array(9)).map((_, i) => i))

    0 讨论(0)
提交回复
热议问题