Initialize a two-dimensional array in JavaScript
This post will discuss how to initialize a two-dimensional array in JavaScript.
A two-dimensional array is an array of arrays, where each sub-array has the same length and represents a row or a column of the array. Here are some of the methods that we can use to initialize a two-dimensional array in JavaScript:
1. Using array literal syntax
The array literal syntax allows us to create an array of arrays using square brackets and commas. To use it, we need to enclose each subarray in square brackets and separate them by commas. We can assign different values to each subarray element. For example, we can write:
|
1 2 3 4 5 6 7 8 |
let arr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; // [[1, 2, 3], [4, 5, 6], [7, 8, 9]] console.log(arr); |
In this example, arr is a two-dimensional array of numbers with three rows and three columns. The first row contains the elements [1, 2, 3], the second row contains [4, 5, 6], and the third row contains [7, 8, 9].
2. Using Nested Loops
Another way to initialize a two-dimensional array is to use nested loops. We can use a nested for loop to create an empty array and then fill each element with another array of a given length. We can assign values to the sub-array elements inside the loop. For example, if we want to create a 3 x 3 matrix of zeros, we can do this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// create an empty array let matrix = []; let rows = 3; // number of rows let cols = 3; // number of columns for (let i = 0; i < rows; i++) { // create an empty sub-array for each row matrix[i] = []; for (let j = 0; j < cols; j++) { // assign 0 to each element matrix[i][j] = 0; } } // [[0, 0, 0], [0, 0, 0], [0, 0, 0]] console.log(matrix); |
3. Using Array constructor
We can use the Array constructor to create an array of a given length and then use the fill() function to fill each element with another array of a given length. We can also use the Array.map() function to assign values to the sub-array elements. For example, if we want to create a 3 x 3 identity matrix, we can do this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// create an array of length 3 and fill it with null let matrix = new Array(3).fill(null); // map each row to a new array matrix = matrix.map((row, i) => { // create an array of length 3 and fill it with 0 row = new Array(3).fill(0); // assign 1 to the diagonal element row[i] = 1; return row; }); // [[1, 0, 0], [0, 1, 0], [0, 0, 1]] console.log(matrix); |
4. Using Array.from() function
We can use the Array.from() function to create an array from an object that has a length property and a function that returns the value for each element. We can use this function to create both the outer and inner arrays. For example, if we want to create a 3 x 3 matrix of random numbers between 0 and 1, we can do this:
|
1 2 3 4 5 6 7 8 |
// create an array of length 3 and map each element to a function let matrix = Array.from({length: 3}, () => { // create an array of length 3 and map each element to a random number return Array.from({length: 3}, () => parseFloat(Math.random().toFixed(2))); }); // [[0.03, 0.21, 0.31], [0.64, 0.75, 0.96], [0.81, 0.11, 0.58]] console.log(matrix); |
That’s all about initialize a two-dimensional array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)