Nested Arrays

Besides basic data types, we can also insert other arrays into arrays. These inserted arrays are called nested arrays.

Nested arrays allow us to store more complex data structures. Let's look at an example:

const studyGroups = [["Jennifer", "Boris"], ["David", "Ava"]];

In this example, we have the studyGroups array, which contains two other arrays, each representing a study group.

Accessing individual elements in a nested array works similarly to a one-dimensional array. For example:

console.log(studyGroups[0]) // Prints: Jennifer,Boris
console.log(studyGroups[0][1]) // Prints: Boris

We see that studyGroups[0] is the element at index 0 in the studyGroups array. That is: ["Jennifer", "Boris"]. And when we want a value from this array, we simply add square brackets with the index of the nested array: studyGroups[0][1]. This first selects the element at index 0 of the studyGroups array, which is ["Jennifer", "Boris"], and in this element, we select the element at index 1, which is "Boris".

Instructions

From the teammates array, print the first teammate in the second group.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

8/10

Nested Arrays | Start Coder