push() a two-dimensional array push() a two-dimensional array arrays arrays

push() a two-dimensional array


You have some errors in your code:

  1. Use myArray[i].push( 0 ); to add a new column. Your code (myArray[i][j].push(0);) would work in a 3-dimensional array as it tries to add another element to an array at position [i][j].
  2. You only expand (col-d)-many columns in all rows, even in those, which haven't been initialized yet and thus have no entries so far.

One correct, although kind of verbose version, would be the following:

var r = 3; //start from rows 3var rows = 8;var cols = 7;// expand to have the correct amount or rowsfor( var i=r; i<rows; i++ ) {  myArray.push( [] );}// expand all rows to have the correct amount of colsfor (var i = 0; i < rows; i++){    for (var j =  myArray[i].length; j < cols; j++)    {        myArray[i].push(0);    }}


You have to loop through all rows, and add the missing rows and columns. For the already existing rows, you loop from c to cols, for the new rows, first push an empty array to outer array, then loop from 0 to cols:

var r = 3; //start from rows 3var c = 5; //start from col 5var rows = 8;var cols = 7;for (var i = 0; i < rows; i++) {  var start;  if (i < r) {    start =  c;  } else {    start = 0;    myArray.push([]);  }  for (var j = start; j < cols; j++) {        myArray[i].push(0);    }}


Iterating over two dimensions means you'll need to check over two dimensions.

assuming you're starting with:

var myArray = [    [1,1,1,1,1],    [1,1,1,1,1],    [1,1,1,1,1]]; //don't forget your semi-colons

You want to expand this two-dimensional array to become:

var myArray = [    [1,1,1,1,1,0,0],    [1,1,1,1,1,0,0],    [1,1,1,1,1,0,0],    [0,0,0,0,0,0,0],    [0,0,0,0,0,0,0],    [0,0,0,0,0,0,0],];

Which means you need to understand what the difference is.

Start with the outer array:

var myArray = [    [...],    [...],    [...]];

If you want to make this array longer, you need to check that it's the correct length, and add more inner arrays to make up the difference:

var i,    rows,    myArray;rows = 8;myArray = [...]; //see first example abovefor (i = 0; i < rows; i += 1) {    //check if the index exists in the outer array    if (!(i in myArray)) {        //if it doesn't exist, we need another array to fill        myArray.push([]);    }}

The next step requires iterating over every column in every array, we'll build on the original code:

var i,    j,    row,    rows,    cols,    myArray;rows = 8;cols = 7; //adding columns in this timemyArray = [...]; //see first example abovefor (i = 0; i < rows; i += 1) {    //check if the index exists in the outer array (row)    if (!(i in myArray)) {        //if it doesn't exist, we need another array to fill        myArray[i] = [];    }    row = myArray[i];    for (j = 0; j < cols; j += 1) {        //check if the index exists in the inner array (column)        if (!(i in row)) {            //if it doesn't exist, we need to fill it with `0`            row[j] = 0;        }    }}