How to create multiple vectors dynamically with Javascript?

Asked

Viewed 207 times

0

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

  • 1

    Could Matthew be clearer about where you’re going with this? Maybe you don’t even have to create 64 vectors. Welcome to Stackoverflow, it would be interesting to read the tour http://answall.com/tour to better understand the operation of the site.

  • 1

    That crazy old, I already created one also using Jquery. Why don’t you change the name of the question to: How to create a Round Robin algorithm? Besides helping a lot of people you’ll have more people to help you. Also try to add what you have already done and if possible add up how the algorithm works and what is left to finish. You need to add more detail to your question to make it more interesting.

  • Can Matheus explain the problem better? If you explain better what you want to do, you will have better answers to your problem. You can [Dit] the question and gather more information.

  • You managed to solve the problem?

2 answers

2

One solution would be to create an Array of Arrays.

This method of a @Matthew-Crumley could make a vector capable for 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)]

1

One way to do it is like this:

var criarArrays = function(n) {
    var arrays = [];
    for (var i = 0; i < n; i++) {
        arrays[i] = [];
    }
    return arrays;
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.