Jagged arrays in JavaScript
This post will discuss about Jagged arrays in JavaScript.
1. What is Jagged arrays?
A jagged array in JavaScript is an array of arrays that can have different lengths. For example, we can have an array of three elements, where the first element is an array of three numbers, the second element is an array of two numbers, and the third element is an array of four numbers. This is how a jagged array looks like:
|
1 2 3 4 5 |
var jaggedArray = [ [1, 2, 3], // The first element is an array of three numbers [4, 5], // The second element is an array of two numbers [6, 7, 8, 9] // The third element is an array of four numbers ]; |
Here, we have an array of numbers where each sub-array represents a row of a matrix, but the rows can have different numbers of columns. In this example, the first sub-array has three elements, the second sub-array has two elements, and the third sub-array has four elements.
2. Uses of Jagged arrays
Jagged arrays are useful when we need to store data that does not have a fixed or regular structure, such as a table with variable number of columns, or a list of lists with different sizes. For example, we can use a jagged array to store a list of students and their grades, where each student may have a different number of grades. A jagged array may look something like this:
|
1 2 3 4 5 |
var students = [ ["Henry", 90, 85, 95], ["Mia", 80, 75], ["Lucas", 100, 100, 100, 100] ]; |
In this example, the first element of each sub-array is the name of the student, and the rest are their grades. We can use a jagged array to perform operations on the data, such as calculating the average grade for each student or finding the highest grade in the class.
3. Access elements of a jagged array
To access the elements of a jagged array, we can use the bracket notation with two indices: one for the sub-array and one for the element within the sub-array. i.e., the first index specifies the position of the subarray in the jagged array, and the second index specifies the position of the element in the subarray. Here’s an example:
|
1 2 3 4 5 6 7 8 9 |
var jaggedArray = [ [1, 2], [3, 4, 5, 6], [7] ]; console.log(jaggedArray[0][0]); // 1 console.log(jaggedArray[1][2]); // 5 console.log(jaggedArray[2][0]); // 7 |
We can also modify the elements of a jagged array by assigning new values to them using the bracket notation. For example, to change the number 7 to 8 in the jagged array above, we can use jaggedArray[2][0] = 8;.
4. Iterate over Jagged arrays
We can also use loops to iterate over the elements of a jagged array. For example, we can use a nested for loop to print all the elements in a jagged array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var jaggedArray = [ [1, 2], [3, 4, 5], [6] ]; // Loop over the subarrays for (var i = 0; i < jaggedArray.length; i++) { // Loop over the elements in each subarray for (var j = 0; j < jaggedArray[i].length; j++) { // Print the element console.log(jaggedArray[i][j]); } } |
Output:
1
2
3
4
5
6
5. Advantages and Disadvantages of Jagged arrays
Jagged arrays have some advantages and disadvantages compared to regular two-dimensional arrays. Some of the advantages are:
- They can save memory space by avoiding empty cells.
- They can represent data that is naturally irregular, such as a spreadsheet with different number of columns for each row.
- They can be dynamically resized by adding or removing elements from the nested arrays.
Some of the disadvantages are:
- They can be harder to iterate over, since we need to check the length of each row before accessing its elements.
- They can be slower to access, since we need to traverse two levels of arrays instead of one.
- They can be less readable and maintainable, since they require more code and logic to manipulate.
That’s all about Jagged 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 :)