Two dimensional array is commonly known as array of arrays. It is very useful in mathematics while working with matrix or grid. There are different approaches to create two dimensional array in JavaScript, these are -
Creating a 2D Array in JavaScript
A 2D array is created by nesting arrays within another array. Each nested array represents a "row," and each element within a row represents a "column."
JavaScript 2D Array1. Using Array Literal Notation - Most used
The basic method to create two dimensional array is by using literal notation [].
Syntax
const mat = [ [ num11, num12, ... ], [ num21, num22, ... ], ... ]
JavaScript
let mat = [
[ 10, 20, 30 ],
[ 40, 50, 60 ],
[ 20, 50, 70 ]
];
console.log(mat);
Output[ [ 10, 20, 30 ], [ 40, 50, 60 ], [ 20, 50, 70 ] ]
2. Using Array.from() Method
The JavaScript Array from() method returns an Array object from any object with a length property or an iterable object.
Syntax
const mat = Array.from({ length: rows }, () => new Array(columns).fill(0));
JavaScript
const rows = 3;
const cols = 4;
const mat = Array.from({ length: rows }, () => new Array(cols).fill(0));
console.log(mat);
Output[ [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ] ]
3. Using Nested For Loop
We can also use nested for loop to create a 2D array. The loop will iterate rows * cols times.
JavaScript
let mat = [];
let rows = 3;
let cols = 3;
let val = 0
for (let i = 0; i < rows; i++) {
mat[i] = [];
for (let j = 0; j < cols; j++) {
mat[i][j] = val++;
}
}
console.log(mat);
Output[ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ] ]
4. Using Array.fill() Method
In this approach, we will use the fill() method and map method for creating the two-dimensional array in JavaScript.
Example: This example shows the implementation of above-explained approach.
JavaScript
const rows = 3;
const cols = 4;
const mat = Array(rows).fill().map(() => Array(cols).fill(0));
console.log(mat);
Output[ [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ] ]
Accessing Elements in a 2D Array
You can access elements in a 2D array using two indices: the row index and the column index. Remember that JavaScript uses zero-based indexing.
JavaScript
let mat = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
];
console.log(mat[0][1]);
console.log(mat[2][0]);
Modifying Elements in a 2D Array
You can modify specific elements in a 2D array by accessing them with their indices and assigning a new value.
JavaScript
let mat = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
];
mat[1][2] = 100;
console.log(mat);
Output[ [ 10, 20, 30 ], [ 40, 50, 100 ], [ 70, 80, 90 ] ]
Similar Reads
JavaScript 2D Arrays
Generally, in JavaScript, we deal with one-dimensional or linear arrays that contain multiple elements of the same type stored at different memory locations with the same name. But, there are also 2D array concepts that exist in JavaScript. What are 2D Arrays?A 2D array is commonly known as an array
3 min read
JavaScript Array of Arrays
An array of arrays also known as a multidimensional array is simply an array that contains other arrays as its element. This structure allows you to store and organize data in a tabular format or a nested structure, making it easier to work with complex data in your applications.JavaScriptlet mat =
4 min read
Types of Arrays in JavaScript
A JavaScript array is a collection of multiple values at different memory blocks but with the same name. The values stored in an array can be accessed by specifying the indexes inside the square brackets starting from 0 and going to the array length - 1([0]...[n-1]). A JavaScript array can be classi
3 min read
How to traverse 2D arrays in JavaScript?
A two-dimensional array or simply 2D array is a format to store data in a grid format that is rows and columns format. There is no special way of declaring a 2D array in JavaScript, we can simply pass an array as an element inside another array. the array obtained in this method will be created as a
6 min read
String Arrays in Java
A String Array in Java is an array that stores string values. The string is nothing but an object representing a sequence of char values. Strings are immutable in Java, this means their values cannot be modified once created.When we create an array of type String in Java, it is called a String Array
5 min read
JavaScript Multidimensional Array
A multidimensional array in JavaScript is an array that contains other arrays as its elements. These are often used to represent data in a grid or matrix format. In JavaScript, there is no direct syntax for multidimensional arrays, but you can achieve this by creating arrays within arrays.Creating a
4 min read
Array Literals in Java
Literal is just unlikely any constant value that can be assigned to a variable. Illustration: int x = 10; // Here 10 is a literal. Now let us stick to the array literals that are just like primitive and string literals java also allows us to use array literals. Java defines special syntax that allow
3 min read
Reverse an Array in Java
Reversing an Array is a common task in every programming language. In Java, there are multiple ways to reverse an array. We can reverse it manually or by using built-in Java methods. In this article, we will discuss different methods to reverse an array with examples.Let us first see the most common
4 min read
JavaScript Array Interview Questions and Answers
JavaScript Array Interview Questions and Answers contains the list of top 50 array based questions that are frequently asked in interviews. The questions list are divided based on difficulty levels (Basic, Intermediate, and Advanced). This guide covers fundamental concepts, common problems, and prac
15+ min read
R - Array
Arrays are essential data storage structures defined by a fixed number of dimensions. Arrays are used for the allocation of space at contiguous memory locations.In R Programming Language Uni-dimensional arrays are called vectors with the length being their only dimension. Two-dimensional arrays are
12 min read