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 3

var rows = 8;
var cols = 7;

// expand to have the correct amount or rows
for( var i=r; i<rows; i++ ) {
  myArray.push( [] );
}

// expand all rows to have the correct amount of cols
for (var i = 0; i < rows; i++)
{
    for (var j =  myArray[i].length; j < cols; j++)
    {
        myArray[i].push(0);
    }
}
Answer from Sirko on Stack Overflow
๐ŸŒ
SitePoint
sitepoint.com โ€บ javascript
Push() with a 2d array? - JavaScript - SitePoint Forums | Web Development & Design Community
May 28, 2007 - Now I would like to push values into this array, but I canโ€™t get the proper syntax to work. How would I for example convert these statements: images[1][0]='a'; images[1][1]='b'; images[1][2]='c';
Discussions

JavaScript 2-Dimensional Array.push() - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Turn scattered docs and code into trusted AI context. Explore the new Stack Internal platform More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 23, 2015
Creating a two dimensional array using .push method (or best way to do it).
Stefan Cutajar is having issues with: Hi guys, I'm currently working on the last challenge of objects and I'm trying to add an array in an array so then I can loop through it to pr... More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
4
March 2, 2018
How to push values from 1 dimensional array to 2 dimensional array in Javascript? - Stack Overflow
This is my first question on this forum. I am a novice programmer. Currently, I working on an app which returns an English translated sentence of the passed binary string. Here is my code: function More on stackoverflow.com
๐ŸŒ stackoverflow.com
Create a simple TWO dimensional array in JavaScript. I am not just trying to append value in an array - Stack Overflow
//Can someone suggest how do i push a and b into c such that c is a 2-dimensional array? (By using JavaScript) More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Quora
quora.com โ€บ Why-cant-I-push-values-into-this-2D-array-in-JavaScript
Why can't I push values into this 2D array in JavaScript? - Quora
Answer (1 of 6): What you're doing is creating an array with a single empty array in its zeroth position. It's the same as [code js]var f = [[]][/code]. You can find the definition of the [code js]Array[/code] constructor here - Array (MDN). If you need 2D arrays often, you can write a function ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-2d-arrays
JavaScript 2D Array โ€“ Two Dimensional Arrays in JS
November 7, 2024 - In JavaScript, there is no direct syntax for creating 2D arrays as with other commonly used programming languages like C, C++, and Java. You can create two-dimensional arrays in JavaScript through jagged arrays โ€” an array of arrays.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 47603662 โ€บ how-to-push-values-from-1-dimensional-array-to-2-dimensional-array-in-javascript
How to push values from 1 dimensional array to 2 dimensional array in Javascript? - Stack Overflow
function binaryAgent(str) { var binary = [128, 64, 32, 16, 8 , 4, 2, 1]; var k = -1; var matrix = str.split(/\s+/).map(i => Array.from(i)) return matrix.map(a => a.reduce((a, c, i) => { if(c == 1) { a.push(binary[i]) } return a }, [])) } var ar = binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"); console.log(ar)
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 47617982 โ€บ create-a-simple-two-dimensional-array-in-javascript-i-am-not-just-trying-to-app
Create a simple TWO dimensional array in JavaScript. I am not just trying to append value in an array - Stack Overflow
It's quite simple. Just use push function and pass the array as an argument. c.push(a); c.push(b); or push them together like: c.push(a,b); ... If i were to do that, c would result in [1,2,a,b] but I want c to be a two-dimensional array.
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 38042358 โ€บ pushing-values-in-a-2-dimensional-array-in-a-for-loop-in-javascript
Pushing values in a 2-dimensional array in a for loop in Javascript - Stack Overflow
June 26, 2016 - var testArray = [3,4], result = []; for (var i = 0; i < testArray[1]; i++) { result.push([].fill.call(new Array(4), i)); } console.log(JSON.stringify(result)); // [[0,0,0,0],[1,1,1,1],[2,2,2,2],[3,3,3,3]] ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Developers remain willing but reluctant to use AI: The 2025 Developer Survey... ... Stack Overflow chat opening up to all users in January; Stack Exchange chat... 12237 How can I remove a specific item from an array in JavaScript? ... By induction: Every subspace from a finite-dimensional vector space is finite-dimensional also.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 30060050 โ€บ push-to-2d-arrays-in-javascript
jquery - Push to 2D Arrays in Javascript - Stack Overflow
Copyvar availableMaps = [[]]; // Store all maps on the server which can be loaded function getMaps() { $.ajax({ type: "POST", url: "handler.php", data: { 'action': 'getMaps' }, dataType: 'json', success: function(data) { var maps = data['maps']; var counter = 0; $.each( maps, function( key, value ) { // Split map name to fetch the type (ex: arena_badlands would be an 'arena' map) var parts = value.split("_"); var mapType = parts[0]; availableMaps[mapType][counter].push(value); counter++; }); } }); } ... Two dimensional arrays have two numeric indexes.
๐ŸŒ
JavaScript Tutorial
javascripttutorial.net โ€บ home โ€บ javascript array methods โ€บ javascript multidimensional array
JavaScript Multidimensional Array
November 8, 2024 - You can use the array methods such as push() and splice() to manipulate elements of a multidimensional array.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ multidimensional-array
JavaScript Multidimensional Array
You can think of a multidimensional array (in this case, x), as a table with 3 rows and 2 columns. ... You can use an index notation or the push() method to add elements to a multidimensional array.
๐ŸŒ
Plus2Net
plus2net.com โ€บ javascript_tutorial โ€บ array-two-dimension.php
Two dimensional array in JavaScript displaying by for loop
<script> var my_ans = new Array(); // declaring array my_ans.push({0:45,1:55,2:65}); my_ans.push({0:145,1:155,2:165}); my_ans.push({0:245,1:255,2:265}); // displaying the array data // for(i=0;i<3;i++){ document.write("key : " + i + " =>value: " + my_ans[i][0] + ',' +my_ans[i][1] + ',' +my_ans[i][2] + "<br>"); } </script> Output is here
๐ŸŒ
Reddit
reddit.com โ€บ r/gamemaker โ€บ how to array_push a 2d array
r/gamemaker on Reddit: How to array_push a 2d array
April 10, 2024 -

Since I couldn't find anything about this in the documentation or with google, thought I'd make a quick post about it; array_push (and presumably all other similar array functions) can be used with 2d arrays, for example:

array_push(array[0], 42);

If array was previously:

array[0,0] = 0

array[1,0] = 0

it will now be:

array[0,0] = 0

array[0,1] = 42

array [1,0] = 0

Presumably this would work for any multidimensional array (push to array[n][n] to add to 3rd dimension of a 3d array, etc.)

Top answer
1 of 3
2
array_push (myArray, X) pushes X into myArray, basically adding it to the end of array. pushing another array to an array would effectively create a 2D array. array_push (myArray, secondArray). Now second Array is in the myArray's end. pay attention to how you write array positions. array[0,0] is an alternative syntax which is confusing as hell imo. I suggest you use the industry standard array[n][n] syntax as it is not only similiar to other programming languages but also documentation states that array[n,n] only works for 2D arrays which is pretty confusing to have in your code if you use 3D or 4D arrays. myArray[0,0] = 42 is myArray[0][0] = 42 which is 'push "42" in the 0th place of the array which is in the 0th place of "myArray"' which returns myArray = [ [ 42 ] ] (if the array was empty prior) same can be done with array_push (array[0],42). BUT ONLY : we trying to use an array[n] as a variable in a function, and the function accepts only arrays. so array[n] must already be an array or the function will result in an error. for an example this would return an error myArray =[]; arrayPush(myArray[0],32); since myArray[0] is not an array, because myArray is empty. This would also return an error : myArray =[0]; arrayPush(myArray[0],32); since myArray[0] is not an array, because it is 0. This would also return an error : myArray =[0]; arrayPush(myArray[1],anotherArray); since myArray[1] is not an array, it does not exist, the [1]th place of myArray is empty. but this can work myArray=[[],[]] // declare a 2D array with 2 1D empty arrays inside beforehand array_push (array[0],42) or array_push (array[1],42) both work now. What exactly is your usecase here?
2 of 3
2
Easiest method is to push array into array, and ignore 2D arrays whatsoever. Not only do you not really need GM's 2D arrays, my example works in nearly every other programming language as well. var newcoords = [5, 3] array_push(coords, newcoords) this turns the format of array into coords[index][x] (subelement 0) and coords[index][y] (subelement 1) so, coords[0][0] would be now 5 and coords[0][1] would be 3. But you probably need to retrieve them via long way as well - I'm not sure. In Javascript, you can do array[][] but in GM, I guess not: var getcoords = coords[0] // returns [5, 3] x = getcoords[0] // returns 5 y = getcoords[1] // returns 3 Or you could use pipe operator to make it much shorter. Look it up in the manual
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ javascript multidimensional array push value | code
JavaScript multidimensional array push value | Code
August 28, 2023 - Use the push() method to add one or more elements to the end of a multidimensional array in JavaScript. This method returns the new length of the array. ... A simple example code will add it to the end of the array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-create-two-dimensional-array-in-javascript
How to Create Two Dimensional Array in JavaScript? - GeeksforGeeks
November 8, 2024 - Here are some of the common methods: Using loop Using Array.from() methodUsing jQuery Method 1: Using LoopsThe for loop or while loop iterates through all the integers between the two given numbers and pushes them into an array.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is a multidimensional array in javascript?
Multidimensional Array in JavaScript - Scaler Topics
July 14, 2024 - A multidimensional array is not present natively in JavaScript. Hence there is no direct way to create a multidimensional array in JavaScript. However, We can define an array of elements to create a multi-dimensional array in such a way so that each element stored in an array is also another array.