Nested Arrays in JavaScript
Introduction When working with arrays in JavaScript, we usually start with a simple list of values. This works well when all the values belong to the same group. Sometimes, we may want to group those values while still keeping them in a single array. For example: const numbers = [ [ 10 , 20 , 30 ], [ 40 , 50 , 60 ] ]; Here, the numbers are organized into two groups. Each group is an array, and…
In JavaScript, arrays are commonly used to store a collection of values. While simple arrays can contain elements of the same type, sometimes it's beneficial to organize values into groups within a single array. This is achieved by creating nested arrays.
A nested array consists of multiple arrays, each containing its own set of values. For instance, consider the following example:
```javascript
const numbers = [ [ 10, 20, 30 ], [ 40, 50, 60 ] ];
```
In this case, the `numbers` array holds two groups of numbers. Each group is an array, and both arrays are nested within the `numbers` array. To illustrate this concept further, let's examine a regular array first:
```javascript
const fruits = [ "Apple", "Banana", "Orange" ];
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);
```
The output of this code is:
```
Apple
Banana
Orange
```
Here, each fruit has an index representing its position in the array. In JavaScript, the index starts at 0, so "Apple" is at index 0, "Banana" at index 1, and "Orange" at index 2.
Now, imagine we want to split the fruits into smaller groups:
```javascript
const fruits = [ ["Apple", "Banana"], ["Orange", "Mango"] ];
console.log(fruits[0]);
console.log(fruits[1]);
```
The output becomes:
```
["Apple", "Banana"]
["Orange", "Mango"]
```
In this nested array, the outer array holds two inner arrays, each representing a group of fruits. Accessing data in a nested array requires multiple indices. For example, to access the first group of numbers, we could use:
```javascript
const numbers = [ [10, 20, 30], [40, 50, 60] ];
console.log(numbers[0]);
```
This code outputs:
```
[10, 20, 30]
```
The first index, `0`, retrieves the first inner array. To extract a specific value, like the number 20, we need a second index:
```javascript
console.log(numbers[0][1]);
```
This line outputs:
```
20
```
Breaking it down, `numbers[0]` retrieves the first inner array `[10, 20, 30]`, and `numbers[0][1]` accesses the second value from that array, which is 20.
The concept of nested arrays can be applied to various data types. For example, grouping student names might look like this:
```javascript
const students = [ ["Hana", "Dini"], ["Khalisha", "Khayra"] ];
console.log(students[0][0]);
console.log(students[1][1]);
```
The output is:
```
Hana
Khayra
```
In this example, `students[0][0]` retrieves the first name from the first inner array, "Hana". Similarly, `students[1][1]` retrieves the second name from the second inner array, "Khayra".
To access a value in a nested array, we start from the outermost array and progress inward with each index. The syntax `array[firstIndex][secondIndex]` allows us to navigate through multiple levels of nesting. This approach becomes more intuitive as we gain familiarity with working with nested arrays in JavaScript.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.