How to create multiple vectors dynamically with Javascript? [closed]

0

I get a value on the input screen, and I need to construct the number of arrays according to the number I received. Example: I get 64 in the input, so I need to create 64 arrays

    
asked by anonymous 15.04.2016 / 21:20

2 answers

2

One solution would be to create an Array of Arrays.

This method of a matthew- crumley could make a vector capable of a variable number of vectors.

function createArray(length) {
    var arr = new Array(length || 0),
        i = length;

    if (arguments.length > 1) {
        var args = Array.prototype.slice.call(arguments, 1);
        while(i--) arr[length-1 - i] = createArray.apply(this, args);
    }

    return arr;
}

createArray();     // [] or new Array()

createArray(2);    // new Array(2)

createArray(3, 2); // [new Array(2),
                   //  new Array(2),
                   //  new Array(2)]
    
15.04.2016 / 21:28
1

One way to do this is like this:

var criarArrays = function(n) {
    var arrays = [];
    for (var i = 0; i < n; i++) {
        arrays[i] = [];
    }
    return arrays;
};
    
15.04.2016 / 21:28