...

/

Nested Arrays

Nested Arrays

Learn to use arrays of arrays in JavaScript.

What is a nested array?

In JavaScript, a nested array is an array that contains other arrays as its values or members. The container array is termed an outer array, and the member array is termed an inner array. An array is said to be a nested array if it has another array as one or more of its members, even if all other members of the outer array are not arrays.

Can you think of some instances where we might have been using a structure same as a multi-dimensional array? One such example is a crossword puzzle!

The general concepts of two-dimensional arrays, n-dimensional arrays, or jagged arrays are implemented as nested arrays in JavaScript. The following program illustrates the structure of a nested array:

The following illustration shows the structure of the array above:

Here’s another example of the structure of a nested array:

The following illustration shows the structure of the list above:

Here’s another example of a nested list:

The following illustration shows the structure of the list above.

We can also create a new array, dlist, with the help of all the arrays above (alist, blist, and clist) using list initialization.

In the code above:

  • We create three arrays—alist, blist, and clist.

  • We create a new array, dlist, with the help of the other arrays.

A mathematical matrix is implemented as a nested array in JavaScript, as shown in the following example: matrixA=[123456789101112] matrixA = \begin{bmatrix} 1&2&3&4 \\ 5&6&7&8 \\ 9&10&11&12 \end{bmatrix}

The variable, matrixA, is an array of three elements, and each element is itself an array of four elements.

The following code demonstrates a way to write values in the forms of rows and columns:

The result of the second program is exactly the same as the first program. Inside the computer’s memory, both variables, matrixA and matrixB, are stored in the same way—as linear sequences.

Individual values in nested arrays

The individual values in a nested list are accessed through multiple index numbers used in a row. Let’s take the example of the following array:

let list1 = [0, 1, [20, 21, [220, 221]], 3, 4];
  • list1 is a nested array.

  • The value, 20, can be accessed as list1[ 2 ][ 0 ], and the value 221 can be accessed as list1[2][2][1].

  • The values that are not nested in list1 can be accessed with a single index number. For example, list1[4] is 4.

  • The number of indexes depends on the level of nesting.

The following illustration shows the structure of the array above. The main level elements are list1[0] , list1[1], list1[3], and list1[4]. The element, list1[2], has another sublist, and the elements of the sublist can be accessed as list1[2][0] and list1[2][1]. The last element, list1[2][2], has another sublist, and the elements can be accessed as list1[2][2][0] and list1[2][2][1].

The following program calculates the sum of two matrix variables:

In the program above:

  • We declare and initialize two matrixes, a and b.

  • We define another matrix, c, which has been initialized with zeros.

  • We traverse the individual values of b to add in the corresponding values of c.

  • The variable, rows, is 3, which represents the number of rows of the matrix.

  • The variable, cols, is 4, which represents the number of columns of the matrix.

Practice nested arrays

Here are a few example programs to practice using nested arrays in JavaScript. By clicking the “Show Solution” button, you’ll find a program that solves the respective problem. You can copy and paste the given solution into the code widget to make sure the output of your solution matches the given solution. There can be several ways of writing correct solutions in programming.

Remember The “Run” button becomes the “Stop” button when we run the code. We have to click the “Stop” button to edit the code. We can re-run the code by clicking on the “Run” button again, after editing. We can re-run to try different inputs of variables.

How to input: For input-based programs, once you run the program, it will require your input to execute. A pop-up box will appear to type the input in that box first to get the desired results. You must type the inputs one after the other for more than one input.

Calculate the sum of each row in a matrix

Write a program to show the sum of each row of the matrix. The matrix must have two rows and three columns.

Sample input 1

[[10,20,30],
  [40,50,60]]

Sample output 1

Matrix: [[10, 20, 30], [40, 50, 60]]
Sum of Row1: 60
Sum of Row2: 150

Sample input 2

[[1,2,3],
  [4,5,6]]

Sample output 2

Matrix: [[1, 2, 3], [4, 5, 6]]
Sum of Row1: 6
Sum of Row2: 15
Javascript (babel-node-es2024)
// Write your code here.

Calculate the sum of each column in a matrix

Write a program to show a matrix in the form of rows and columns, and then show the sum of each column. The matrix must have five rows and ten columns.

Sample input

[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
 [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
 [40, 41, 42, 43, 44, 45, 46, 47, 48, 49]]

Sample output

Displaying in the matrix form:
   Row no.0   ==> 0,1,2,3,4,5,6,7,8,9
   Row no.1   ==> 10,11,12,13,14,15,16,17,18,19
   Row no.2   ==> 20,21,22,23,24,25,26,27,28,29
   Row no.3   ==> 30,31,32,33,34,35,36,37,38,39
   Row no.4   ==> 40,41,42,43,44,45,46,47,48,49
Column Sums are: 100,105,110,115,120,125,130,135,140,145

Access this course and 1200+ top-rated courses and projects.