This is pretty cut and dry, just set up a nested loop:

var count = 1;
var twoDimensionalArray =[];

for (var i=0;i<2;i++)
{
  var data = [];
  for (var j=0;j<5;j++)
  {
    data.push("Test" + count);
    count++;
  }

  twoDimensionalArray.push(data);

}
Answer from Brian on Stack Overflow
๐ŸŒ
Zipy
zipy.ai โ€บ blog โ€บ how-can-i-create-a-two-dimensional-array-in-javascript
how can i create a two dimensional array in javascript
April 12, 2024 - Dynamic Resizing: Arrays in JavaScript are dynamic. You can add or remove rows/columns by using array methods like push, pop, shift, and unshift. Performance Considerations: For large 2D arrays, consider the impact of operations on performance.
Discussions

How to create a dynamic 2D array in javascript - Stack Overflow
Ofc. I mean, the topic starter operates with this category. He wants to use array as two-dimensional array, not as array of arrays. As long as JS is weakly-typed language, you cannot guarantee that every value of first-level array is array itself. So it's not two-dimensional array at all. More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 22, 2013
javascript - How to create dynamic two dimensional array in jquery or js - Stack Overflow
I need to create global two dimensional array in jquery or javascript ... On click of that button I am getting this error "globalArray[0] is undefined" How can I create global dynamic ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 25, 2017
javascript multi dimensional dynamic array - Stack Overflow
I could use some guidance on specifying a dynamic multi dimensional array in javascript. I understand that javascript does not natively define multi dim arrays, but rather an array of an array, ie.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - constructing two dimensional array dynamically - Stack Overflow
I have some values in JavaScript array as shown var sampledata = {10,20,30,40};// these values would come from database later I want to create a two dimensional array with these values. I want to More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Wikitechy
wikitechy.com โ€บ tutorials โ€บ javascript โ€บ create-a-two-dimensional-array-in-javascript
javascript tutorial - create a two dimensional array in JavaScript - By Microsoft Award MVP - Learn in 30sec javascript - java script | wikitechy
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)]...
๐ŸŒ
Medium
medium.com โ€บ @ryan_forrester_ โ€บ two-dimensional-arrays-in-javascript-how-to-guide-8faa070abab5
Two-Dimensional Arrays in JavaScript (How to Guide) | by ryan | Medium
September 12, 2024 - A two-dimensional array in JavaScript can be created by nesting arrays within an outer array. const matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; You can also create a two-dimensional array dynamically using loops.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ examples โ€บ create-two-dimensional-array
JavaScript Program to Create Two Dimensional Array | Vultr Docs
December 20, 2024 - Use loops to generate both dimensions of the array. This approach is useful when the array dimensions are determined at runtime. ... var rows = 4; var cols = 5; var dynamicArray = []; for(var i = 0; i < rows; i++) { dynamicArray[i] = []; for(var ...
๐ŸŒ
W3docs
w3docs.com โ€บ javascript
How to Create a Two Dimensional Array in JavaScript | W3Docs
To create a two-dimensional array in JavaScript, you can use an array of arrays.
Find elsewhere
๐ŸŒ
DEV Community
dev.to โ€บ yanhaijing โ€บ mastering-javascript-multiple-ways-to-generate-a-two-dimensional-array-cpg
Mastering JavaScript: Multiple Ways to Generate a Two-Dimensional Array - DEV Community
April 13, 2024 - There are many methods to generate two-dimensional arrays, and today we'll explore the following, analyzing their pros and cons: ... This is the most straightforward method. First, create an outer array, then create an inner array at each position.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ two-dimensional-array
JavaScript Program to Create Two Dimensional Array
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // program to create a two dimensional array function twoDimensionArray(a, b) { let arr = []; // creating two dimensional array for (let i = 0; i< a; i++) { for(let j = 0; j< b; j++) ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-create-two-dimensional-array-in-javascript
How to Create Two Dimensional Array in JavaScript? - GeeksforGeeks
November 8, 2024 - The basic method to create two dimensional array is by using literal notation []. ... The JavaScript Array from() method returns an Array object from any object with a length property or an iterable object.
๐ŸŒ
Bluera
ueck.bluera.it โ€บ how-to-create-two-dimensional-array-in-javascript-dynamically.html
How to create two dimensional array in javascript dynamically
In this tutorial, you'll learn how to create JSON Array dynamically using JavaScript. This is one of most common scenarios and you'll see two ways of creating JSON array dynamically. The first method will use for loop for creating JSON array from an input array. Jan 16, 2001 ยท Sorting a Two-Dimensional Array with Bubble Sort This article assumes that you have read my previous article on Bubble Sort with a one-dimensional array.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-2d-arrays
JavaScript 2D Array โ€“ Two Dimensional Arrays in JS
November 7, 2024 - You can create two-dimensional arrays in JavaScript through jagged arrays โ€” an array of arrays.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how can i create a two-dimensional array in javascript?
How can I create a two-dimensional array in JavaScript? | Sentry
February 15, 2023 - The Array() constructor can be called with or without the new keyword. Both constructions create an array instance. Youtube How Sentry.io saved me from disaster (opens in a new tab) Resources Improve Web Browser Performance - Find the JavaScript ...
Top answer
1 of 5
23

You do not Dim the Array to an actual size,

You can just set an variable on any index to whatever you want

if you want two Dimensions you just could define an array

var arr = []

set an element to another array

arr[0] = []

and put an value inside an element in the "2nd dimension"

arr[0][10] = 10

You only have the elements in the "first dimension" to be an array

You could also do things like placing arrays into arrays

like

var arr = []
var arr1 = [1,2,3]
arr[0] = arr1
console.log(arr[0][2]) //3

And if you have an array and want to push elements in an inner array just do a check if the element is defined, like

var arr = []
for ( var i = 0; i < 5 ; i++) {
    if(!arr[i])
        arr[i] = []
    for ( var j = 0; j < 3 ; j++)
        arr[i][j] = i + " - " + j // Or like this,
        //arr[i].push(i + " " + j) // 
}
console.log( arr[2][3]) // 2 - 3

Given the Code you posted

function newTest() {
    var myArray = [[],[]];
  //  myArray[] = new Array(14);

    var recCount = 15
    var n =0;

    var i = 0;
    var s = 0;

    for (i = 0; i < recCount; i++) {
        if(!myArray[i])
             myArray[i] = []
        for (s = 0; s < 15; s++) {
            console.log(i)

          //  myArray[i] = new Array(14);

            myArray[i][s] = s;
            n++;

          //  alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));

        }
        // var u = 4;
        s = 0;

        alert(myArray[0][3]);
        alert(myArray[0][4]);

        alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));              

    }
}
newTest()

This works for me

โ€‹

2 of 5
3

All arrays are dynamic in javascript. You can do things like

var list = [];
list[15] = 15;

And it will simply expand the array to have a length of 16, with the values for the first 15 indexes being undefined (~ null). Thus, your 3 examples of multi-dimensional arrays should all work.

Edit: Just saw that you included your code. You are not initializing the arrays of your first dimension. Your loop should be

for (i = 0; i < recCount; i++) {

    //If the outer array does not have an inner array at this position already, create one
    if (!myArray[i])
        myArray[i] = []; //!!!

        for (s = 0; s < 15; s++) {
          //  myArray[i] = new Array(14);

            myArray[i][s] = coordsAry[n]; //myArray[i] returned null/undefined, when i got bigger then the declared array.
            n++;

          //  alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));

        }
        // var u = 4;
        s = 0;

        alert(myArray[0][3]);
        alert(myArray[0][4]);

        alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));              

    }
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ curriculum help
Creating 2D array in javascript - Curriculum Help - The freeCodeCamp Forum
January 9, 2023 - Hi All, I have a below code in which I am trying to fill the data from table that has few rows and columns. The problem is that I am unable to get the right value at indexes outside the loop. I need to add values at each row of the array and return the array to use in other functions.