1

I'm working on Google Script and I'm testing different ways to create two dimensions arrays. I have created an array like this:

  var codes = new Array(6);
  for (var i = 0; i < 6; i++) {
  codes[i] = new Array(4);
  }
  codes[0][0]="x"; 
  codes[0][1]="x"; 
  codes[0][2]="x"; 
  codes[0][3]="x";
  codes[1][0]="x";
  codes[1][1]="x";
  codes[1][2]="x"; 
  codes[1][3]="x";
  codes[2][0]="x";
  codes[2][1]="x";
  codes[2][2]="x";
  codes[2][3]="x";
  codes[3][0]="x";
  codes[3][1]="x";
  codes[3][2]="x";
  codes[3][3]="x";
  codes[4][0]="x";
  codes[4][1]="x";
  codes[4][2]="x";
  codes[4][3]="x";
  codes[5][0]="x";
  codes[5][1]="x";
  codes[5][2]="x";
  codes[5][3]="x";

And it is working fine.

I read following links here, here and here.

But when I do it like this:

var codes = new Array(6);
  for (var i = 0; i < 6; i++) {
  codes[i] = new Array(4);
  }
codes[0]=["x","x","x","x"];
codes[1]=["x","x","x","x"];
codes[2]=["x","x","x","x"];
codes[3]=["x","x","x","x"];
codes[4]=["x","x","x","x"];
codes[5]=["x","x","x","x"];

It didn't work, so I tried like this:

var codes = new Array([["x","x","x","x"],["x","x","x","x"],["x","x","x","x"],["x","x","x","x"],["x","x","x","x"],["x","x","x","x"]]);

it didn't work either.

When the code don't work, I get no error, just no display of the values.

What am I doing wrong? It looks to be the same code and the two not working ways are recommended in many documentations.

Community
  • 1
  • 1
Pauline
  • 35
  • 6

2 Answers2

0

W3schools says that there is no need to use new Array(). For simplicity, readability and execution speed, use literal method ex:

var animals = ["cat", "rabbit"];

Reason why your code was not working is that you're equaling codes inside the loop and after end of loop scope 'codes' is getting only the last set array. Instead you should push those arrays to codes.

var codes = [];
for (var i = 0; i < 6; i++) {
  codes.push([i]);
}
console.log(codes)
codes[0]=["x","x","x","x"];
codes[1]=["x","x","x","x"];
codes[2]=["x","x","x","x"];
codes[3]=["x","x","x","x"];
codes[4]=["x","x","x","x"];
codes[5]=["x","x","x","x"];
RegarBoy
  • 3,228
  • 1
  • 23
  • 44
0

Better yet, two for loops to create the double array:

var codes = [], // Initiate as array, in Javascript this is actually fastre than using new (I don't know any cases you should use new)
    rows = 6,
    columns = 6;

for (var i = 0; i < rows; i++){
    codes.push([]); // Initiate 
    for (var j = 0; j < columns; j++){
        codes[i][j] = 'x';
    }
}

Other idea, pre-initiate an array with the correct columns then copy:

var arrTemp = [],
    codes = [],
    rows = 6,
    columns = 6;

for (var j = 0; j < columns; j++)
    arrTemp[i] = 'x';

for (var i = 0; i < rows; i++)
    codes.push( arrTemp.slice(0) ); // If you just push the array without slice it will make a reference to it, not copy

Other way to pre-initiate the array with 'x's:

arrTemp = Array.apply(null, Array(columns)).map(function () {return 'x'});
Kriggs
  • 3,731
  • 1
  • 15
  • 23