How to generate sequence of numbers/chars in javascript?

前端 未结 18 1180
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 06:22

Is there a way to generate sequence of characters or numbers in javascript?

For example, I want to create array that contains eight 1s. I can do it with for loop, bu

相关标签:
18条回答
  • 2020-12-13 06:53

    Using Jquery:


    $.map($(Array(8)),function(val, i) { return i; })
    

    This returns:

    [0, 1, 2, 3, 4, 5, 6, 7]
    

    $.map($(Array(8)),function() { return 1; })
    

    This returns:

    [1, 1, 1, 1, 1, 1, 1, 1]
    

    0 讨论(0)
  • 2020-12-13 06:53

    If like me you use linspace a lot, you can modify your version of linspace easily like so:

    function linSeq(x0, xN) {
        return linspace(x0, xN, Math.abs(xN-x0)+1);
    }
    
    function linspace(x0, xN, n){
    
        dx = (xN - x0)/(n-1);
        var x = [];
        for(var i =0; i < n; ++i){
            x.push(x0 + i*dx);
        }
    
        return x;
    }
    

    You can then use linSeq in any direction, e.g. linSeq(2,4) generates 2,3,4 while linSeq(4,2) generates 4,3,2.

    0 讨论(0)
  • 2020-12-13 06:54

    Typescript method based on Ariya Hidayat code:

    /**
     * Generates sequence of numbers from zero.
     * @ param {number} count Count of numbers in result array.
     * @ return {Array<number>} Sequence of numbers from zero to (count - 1).
     */
    public static sequence(count: number): Array<number>
    {
        return Array.apply(0, Array(count)).map((x, i) =>
        {
            return i;
        });
    }
    
    0 讨论(0)
  • 2020-12-13 06:55
    The fastest way to define an array of 8 1s is to define it-
    var A= [1, 1, 1, 1, 1, 1, 1, 1];
    
    // You'd have to need a lot of 1s to make a dedicated function worthwhile.
    
    // Maybe in the Matrix, when you want a lot of Smiths:
    
    Array.repeat= function(val, len){
        for(var i= len, a= []; i--; ) a[i]= val;
        return a;
    }
    var A= Array.repeat('Smith',100)
    
    /*  returned value: (String)
    Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith
    */
    
    0 讨论(0)
  • 2020-12-13 06:57

    Without a for loop, here is a solution:

    Array.apply(0, Array(8)).map(function() { return 1; })
    

    The explanation follows.

    Array(8) produces a sparse array with 8 elements, all undefined. The apply trick will turn it into a dense array. Finally, with map, we replace that undefined the (same) value of 1.

    0 讨论(0)
  • 2020-12-13 06:57
    var GetAutoNumber = exports.GetAutoNumber = (L) => {
        let LeftPad = (number, targetLength) => {
            let output = number + '';
            while (output.length < targetLength) {
                output = '0' + output;
            }
            return output;
        }
        let MaxNumberOfGivenLength = "";
        for (let t = 0;t < L;t++) {
            MaxNumberOfGivenLength = MaxNumberOfGivenLength + "9"
        }
        let StartTime = +new Date();
        let Result = [];
        let ReturnNumber;
        for (let i = 1;i <= MaxNumberOfGivenLength;i++) {
            Result.push(LeftPad(i, L))
        }
        for (let k = 0;k != 26;k++) {
            for (let j = 0;j <= 999;j++) {
                Result.push(String.fromCharCode(k + 65) + LeftPad(j, (L - 1)));
            }
        }
        console.log(Result.length)
        return Result;
    }
    GetAutoNumber(3)
    

    It will generate result like 001-999, A01-A99... Z01-Z99

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