How can I create a two dimensional array in JavaScript?

后端 未结 30 4345
天涯浪人
天涯浪人 2020-11-21 05:25

I have been reading online and some places say it isn\'t possible, some say it is and then give an example and others refute the example, etc.

  1. How do I dec

相关标签:
30条回答
  • 2020-11-21 05:41

    Here's a quick way I've found to make a two dimensional array.

    function createArray(x, y) {
        return Array.apply(null, Array(x)).map(e => Array(y));
    }
    

    You can easily turn this function into an ES5 function as well.

    function createArray(x, y) {
        return Array.apply(null, Array(x)).map(function(e) {
            return Array(y);
        });
    }
    

    Why this works: the new Array(n) constructor creates an object with a prototype of Array.prototype and then assigns the object's length, resulting in an unpopulated array. Due to its lack of actual members we can't run the Array.prototype.map function on it.

    However, when you provide more than one argument to the constructor, such as when you do Array(1, 2, 3, 4), the constructor will use the arguments object to instantiate and populate an Array object correctly.

    For this reason, we can use Array.apply(null, Array(x)), because the apply function will spread the arguments into the constructor. For clarification, doing Array.apply(null, Array(3)) is equivalent to doing Array(null, null, null).

    Now that we've created an actual populated array, all we need to do is call map and create the second layer (y).

    0 讨论(0)
  • 2020-11-21 05:46

    To create an 4x6 array, simply do this

    const x = [...new Array(6)].map(elem => new Array(4))
    

    It's usually a good practice to start with an empty array, rather than filling w random values. (You normally declare array as const x = [] in 1D, so better to start w empty in 2D.)

    0 讨论(0)
  • 2020-11-21 05:47

    var items = [
      [1, 2],
      [3, 4],
      [5, 6]
    ];
    console.log(items[0][0]); // 1
    console.log(items[0][1]); // 2
    console.log(items[1][0]); // 3
    console.log(items[1][1]); // 4
    console.log(items);

    0 讨论(0)
  • 2020-11-21 05:47

    I had to make a flexible array function to add "records" to it as i needed and to be able to update them and do whatever calculations e needed before i sent it to a database for further processing. Here's the code, hope it helps :).

    function Add2List(clmn1, clmn2, clmn3) {
        aColumns.push(clmn1,clmn2,clmn3); // Creates array with "record"
        aLine.splice(aPos, 0,aColumns);  // Inserts new "record" at position aPos in main array
        aColumns = [];    // Resets temporary array
        aPos++ // Increments position not to overlap previous "records"
    }
    

    Feel free to optimize and / or point out any bugs :)

    0 讨论(0)
  • 2020-11-21 05:48

    You simply make each item within the array an array.

    var x = new Array(10);
    
    for (var i = 0; i < x.length; i++) {
      x[i] = new Array(3);
    }
    
    console.log(x);

    0 讨论(0)
  • 2020-11-21 05:48

    Similar to activa's answer, here's a function to create an n-dimensional array:

    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)]
    
    0 讨论(0)
提交回复
热议问题