Construct an array of arrays in JavaScript
This post will discuss how to create an array of arrays in JavaScript.
There are several ways to construct an array of arrays in JavaScript, which is a data structure that can store multiple arrays as its elements. An array of arrays is also known as a two-dimensional array or a matrix. Here are some common functions:
1. Using an array literal
This is the easiest and most common way to create an array of arrays. An array literal is a pair of square brackets [] that can contain comma-separated values. To create an array of arrays, we just need to use the brackets with nested arrays as values. Here’s an example:
|
1 2 3 4 5 |
// an array of three subarrays let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; // [[1, 2, 3], [2, 4, 6], [3, 6, 9]] console.log(arr); |
2. Using Array constructor
This is another way to create an array of arrays, but it is less preferred than the array literal. The Array constructor is a function that can create an array object with a given length or elements. To create an array of arrays, we can either use the constructor with nested arrays as arguments or with a single argument that specifies the length of the outer array. Here’s an example:
|
1 2 3 4 5 |
// create an array of arrays using the Array constructor with nested arrays as arguments let arr = new Array([1, 2, 3], [2, 4, 6], [3, 6, 9]); // [[1, 2, 3], [2, 4, 6], [3, 6, 9]] console.log(arr); |
3. Using Array.push() function
The Array.push() function adds one or more elements to the end of an array, and returns the new length of the array. We can use this function to append subarrays to an existing array. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// an empty array let arr = []; // add a subarray at the end arr.push([1, 2, 3]); // add another subarray at the end arr.push([4, 5, 6]); // add one more subarray at the end arr.push([7, 8, 9]); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]] console.log(arr); |
4. Using Array.of() function
The Array.of() function creates a new array instance from a variable number of arguments. We can use this function to create an array of arrays by passing subarrays as arguments. Here’s an example:
|
1 2 3 4 5 |
// an array of three subarrays let arr = Array.of([1, 2, 3], [4, 5, 6], [7, 8, 9]); // [[1, 2, 3], [2, 4, 6], [3, 6, 9]] console.log(arr); |
5. Using a loop
We can use a loop to create an array of arrays dynamically, based on some logic or condition. For example, we can use a for loop to create an array of arrays that contain the multiplication table from 1 to 10:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// an empty array let arr = []; // loop from 1 to 3 for (let i = 1; i <= 3; i++) { // a subarray for each row let subarr = []; // loop from 1 to 3 for (let j = 1; j <= 3; j++) { // multiply i and j and push to subarray subarr.push(i * j); } // push subarray to main array arr.push(subarr); } // [[1, 2, 3], [2, 4, 6], [3, 6, 9]] console.log(arr); |
6. Using Array.map() function
To create an array of arrays, we can use the Array.map() function to map each element of an existing array to a subarray. This function creates a new array with the results of calling a function on every element in the original array. The syntax is array.map(function(element, index, array) {return value;}), where function takes an element, its index, and the original array as parameters and returns a value for the new array. Here’s an example:
|
1 2 3 4 5 6 7 8 |
// create an existing array let arr = [1, 2, 3]; // map each element to a subarray let newArr = arr.map(x => [x * 10, x * 20, x * 30]); // [[10, 20, 30], [20, 40, 60], [30, 60, 90]] console.log(newArr); |
That’s all about creating an array of arrays 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 :)