Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The term "multi-dimensional array" can also describe a two-dimensional array. A list of two-dimensional arrays with related data types is kept in the Array. The two-dimensional Array is set up using a matrix, simply a collection of rows and columns.
In this blog, we will discuss creating a javaScript 2d Array. Let's start going!
A two-Dimensional Array is a collection of elements arranged in a grid-like pattern, with each value identified by a pair of indices representing its row and column positions. It is commonly used to represent data in a tabular format or solve matrix-related programming problems.
Declaration of 2D array: To declare a 2D array in JavaScript, you can use array literals with nested arrays to represent rows and columns. For example:
let myArray = [[]];
This declares a 2D array named myArray with one row (empty) and one column.
Initializing a 2D array:
To initialize a 2D array in JavaScript, you can use nested array literals to define the values for each row and column. For example:
let myArray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
This initializes a 2D array myArray with three rows and three columns, filled with numbers.
Reading a 2D array:
To read values from a 2D array in JavaScript, you can use nested loops to traverse through each row and column, accessing each element individually. For example:
for (let i = 0; i < myArray.length; i++) {
for (let j = 0; j < myArray[i].length; j++) {
console.log(myArray[i][j]);
}
}
This code snippet reads each element from the 2D array myArray and logs it to the console.
For example, the following is a jagged array i.e. array where the inner arrays can have different lengths. The first two rows have four elements, while the third row only has two elements.
filter() and splice() are two built-in functions that help to remove elements from a 2D array. If we need to remove specific elements from the array, then use the splice() method and if we need to remove elements from the array based on some condition, then use the filter() method.
The following example will help you to understand the removal of elements in arrays using both filter() and splice() :
javascript
javascript
const array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; // Remove all elements from the inner arrays that are greater than 5 const newArray = array.map((innerArray) => innerArray.filter((element) => element <= 5)); // If any of the inner arrays are now empty, remove them from the outer array newArray.splice(newArray.findIndex((innerArray) => innerArray.length === 0), 1); // Print the original array console.log("Original array:"); console.log(array); // Print the newArray variable console.log("New array:"); console.log(newArray);
You can also try this code with Online Javascript Compiler
Javascriptis scripting as well as a programming language, suggesting some methods of creating Javascript 2d array are:-
Array Constructor
Array Literal Notation
Array.from() Method
Array.prototype.map() Method
Array Constructor in JavaScript
Javascript array constructor is used to return the constructor function for the array object. It does not return the function's name, only the reference to the function.
In this part of the " Javascript 2d array," we will create a two-dimensional array using Array Constructor and for a loop.
javascript
javascript
const x = 2; const y= 5; let a = new Array(x); // create an empty array of length x for (var i = 0; i < x; i++) { a[i] = new Array(y); // make each element an array } console.log(a);
You can also try this code with Online Javascript Compiler
The map() method builds a new array from the contents of the calling Array after calling a given function on each element.
When an array is created using an array literal, its elements are initialized with the specified values, and the number of arguments specified determines how long the Array will be.
In this part of the “ Javascript 2d array,” we will create a two-dimensional array using Array.from() Method. Any JavaScript object with the length property or an iterable object will return an array object when called using the Array.from() method.
javascript
javascript
const x= 3; const y= 3; let a = Array.from(Array(x), () => new Array(y)); console.log(a);
You can also try this code with Online Javascript Compiler
In our Array, we use square bracket notations to access the elements. JavaScript only permits arrays with integer-based indexes. String-based indexes convert our variable from an array to an object, losing some of its array-specific characteristics.
The syntax for accessing elements:-
array_2_d[0][3] // right syntax
The syntax for accessing the first and last element:-
// This gives us the first element
console.log("First Element : " + array_2_D[0][0]);
// This gives us the last element
console.log("Last Element : " + array_2_D[array_2_D.length -1][(array_2_D[array_2_D.length -1]).length - 1]);
Updating Elements 2D Arrays in JavaScript
We can update the elements in an array like how we access the elements. The correct indices of the elements that need to be updated are:-
The code that follows squares and updates each element in our Array.
True 2D Arrays: True 2D arrays in JavaScript have a fixed number of rows and columns, where each row contains the same number of elements. They are created using nested array literals with consistent dimensions. For example:
Jagged Arrays: Jagged arrays in JavaScript are arrays of arrays where the inner arrays can have varying lengths. This creates a "jagged" appearance when visualized as rows and columns. Jagged arrays are created by defining each inner array separately. For example:
A 2D array in JavaScript is an array of arrays where each element of the main array holds an array of values. It allows for the creation of a grid-like structure, with rows and columns, that can be easily accessed and manipulated using nested loops.
How to create a 2D array in JavaScript?
To create a 2D array in JavaScript, you can declare a variable and assign it an array of arrays, where each inner array represents a row. For Example, array = [[1, 2, 3], [4, 5, 6] be a 2D array of 2 columns and 3 columns.
What is the length of 2D array in JavaScript?
We use .length built-in function to find the length of any array. Let say you have an array: let arr=[[1,2,3,4],[0,9,8,7],[4,5,6,7]] and you have to find the length of arr you have to just write : arr.length, and you will get the length of the array.
How to fill 2D array in JavaScript?
To fill a 2D array in JavaScript, you can use a nested for loop. Iterate over the outer array and then the inner array, setting each element to the desired value.
How to pass 2D array in JavaScript?
To pass a 2D array in JavaScript, simply include it as an argument when calling a function. JavaScript treats arrays as reference types, so modifications made within the function will reflect outside.
What is the difference between array and 2D array?
An array in JavaScript is a collection of elements stored in a single dimension, while a 2D array consists of arrays within an array, forming rows and columns, adding another dimension.
Conclusion
In this blog, we have studied Javascript 2D Array. JavaScript 2D arrays provide a versatile and powerful way to work with tabular data, matrices, and complex structures. Understanding how to declare, initialize, manipulate, and pass 2D arrays in JavaScript opens up opportunities for building dynamic and interactive web applications with rich data representation and manipulation capabilities.
We sincerely hope that this blog has improved your understanding of Javascript 2d Array, and if you want to learn more, then you can check articles on:-
Refer to our guided pathways on Code studio to learn more about DSA, Competitive Programming, System Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.