Stack Overflow
stackoverflow.com › questions › 966225 › how-can-i-create-a-two-dimensional-array-in-javascript
How can I create a two dimensional array in JavaScript? - Stack Overflow
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. How do I declare a 2 dimensional array in JavaScript? (
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-2d-array
JavaScript 2D Array - GeeksforGeeks
July 11, 2025 - The JavaScript Array from() method returns an Array object from any object with a length property or an iterable object. ... const rows = 3; const cols = 4; const mat = Array.from({ length: rows }, () => new Array(cols).fill(0)); console.log(mat); ... We can also use nested for loop to create a 2D array...
Sentry
sentry.io › sentry answers › javascript › how can i create a two-dimensional array in javascript?
JavaScript Two-Dimensional Array: Create a Matrix | Sentry
1 month ago - It takes in an iterable or array-like object to convert to an array as its first argument. The optional second argument, mapFn, is a function that’s called on every item of the array. Its return value is added to the array. In the example code below, each new row created for the 2D array is an array with a length of 5.
Reddit
reddit.com › r/learnjavascript › how to create a two-dimensional array?
r/learnjavascript on Reddit: How to create a two-dimensional array?
July 30, 2022 -
Just started using javascript and I need a 2D-Array (coming from C++ where this is pretty normal) but I am not sure how to declare such an array and initialise it with zeros.
Thank you
Edit: After searching a bit I came across this(if anyone could explain me how it does it I would really appreciate it - I can use it but its better to fully understand it)
let Table = (r,c) => [...Array(r)].map(x=>Array(c).fill(0));
Top answer 1 of 2
2
There is another method on the Array object that I would use, but let's talk about the spread and fill method you have here. [...Array(r)] We call the Array constructor with the argument r, creating an array of r empty slots. Then we spread that array , creating another array of r elements each having the value undefined. This allows us to act on each element. It has the same effect as this: Array(r).fill('blah') We are going to replace each element in the next step anyway. .map(x => /* ... */) Here we .map() each value to some other value. Array(c).fill(0) That other value is itself an array of size c, which we .fill() with zeroes . The result is an array of r arrays of c zeroes. Now here is how I would do it: Array.from() takes as its first argument an array-like object and as its second argument a mapping function. So we can do this: const Table = (r, c) => Array.from( { length: r }, () => Array(c).fill(0) ) Now we are creating and mapping the outer array in one swoop instead of creating, then spreading, then mapping.
2 of 2
1
One way would be as an array of arrays; I guess that is the popular technique. Another way would be to combine the indices as a single string, like `${i}:${j}` and use that to index an object.
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 - Comparatively, one-dimensional arrays are like a single row of that spreadsheet—useful for linear data but limited when dealing with multi-faceted relationships. The transition from 1D to 2D arrays in JavaScript doesn't require learning new syntax but rather understanding how to nest arrays within arrays and manipulate these nested structures effectively.
Programiz
programiz.com › javascript › examples › two-dimensional-array
JavaScript Program to Create Two Dimensional Array
In the above program, the first argument of the twoDimensionArray() function represents the number of array elements, and the second argument represents the number of array elements inside of each array element.
Scaler
scaler.com › home › topics › javascript 2d array
2D Arrays in JavaScript - Scaler Topics
January 1, 2024 - A javascript 2d array is a type of multidimensional array in which we can have multiple arrays and these arrays can store elements of the same data type. All the elements of an array are stored in the form of rows and columns. A 2d array in javascript stores its element in the heap memory.
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 - Mastering the various methods to generate two-dimensional arrays is an essential skill for every JavaScript developer. Through these methods, we can flexibly handle various data structures and algorithm challenges. I hope this article helps you better understand and use these techniques.
YouTube
youtube.com › bro code
Learn 2D ARRAYS in JavaScript in 6 minutes! ⬜ - YouTube
// 2D array = multi-dimensional array that stores a matrix// of data in rows and columns.// Useful for games, spreadshe...
Published: November 6, 2023
Views: 25K