How to Access Nested Arrays and Objects in JavaScript

Reading Time: 4 mins

Introduction

In JavaScript, we often work with complex data structures that contain nested arrays and objects. Accessing these nested elements can be challenging, especially if we don’t know the proper techniques to navigate through them. In this blog post, we will explore how to access nested arrays and objects in JavaScript, along with some sample code to illustrate these concepts.

Before we dive into how to access nested arrays and objects in JavaScript, it’s essential to understand how to define these structures in JavaScript. An array can hold any number of values, including other arrays and objects. Similarly, an object can contain any number of key-value pairs, with each value being an array or object.

Defining Nested Arrays and Objects

Here is an example of a nested array in JavaScript:

const nestedArray = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

In this example, we have defined a nested array with three sub-arrays, each containing three values. Basically, this is the simplest example mentioned here.

Here is an example of a nested object in JavaScript:

const nestedObject = {
  name: "Vishal Kukreja",
  age: 32,
  address: {
    street: "123 Mumbai fashion St",
    city: "Midtown",
    state: "MH",
  },
  hobbies: ["reading", "drawing", "gardening"],
};

In this example, we have defined a nested object with a sub-object for the address key and a sub-array for the hobbies key.

Access Nested Arrays Objects

To access an element in a nested array, we need to specify the index of the sub-array and the index of the element we want to access.

Here’s an example of how to access an element in a nested array:

const nestedArray = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

console.log(nestedArray[1][0]); 

// Output: 4

In this example, we are accessing the value at index 1 of the sub-array at index 0 of the nested array.

To access an element in a nested object, we can use dot notation or bracket notation to access the value of a key in a sub-object. In the same way you can use or the nested one.

Here’s another example of how to access an element in a nested object:

const nestedObject = {
  name: "Vishal Kukreja",
  age: 32,
  address: {
    street: "123 Mumbai fashion St",
    city: "Midtown",
    state: "MH",
  },
  hobbies: ["reading", "drawing", "gardening"],
};

console.log(nestedObject.address.street); 

// Output: "123 Mumbai fashion St"

In this example, we are using dot notation to access the value of the street key in the address sub-object.

Iterating through Nested Arrays

Iterating through nested arrays and objects can be challenging, but JavaScript provides several ways to loop through these structures.

For nested arrays, we can use a nested for loop to iterate through each sub-array and its elements.

Here’s an example of how to iterate through a nested array:

const nestedArray = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

for (let i = 0; i < nestedArray.length; i++) {
  for (let j = 0; j < nestedArray[i].length; j++) {
    console.log(nestedArray[i][j]);
  }
}

//Output:
1
2
3
4
5
6
7
8
9

In this example, we are using a nested for loop to iterate through each sub-array in the nested array. The outer loop iterates through each sub-array, and the inner loop iterates through each element in the sub-array.

Iterating through Nested Objects

To iterate through a nested object in JavaScript, we can use a for…in loop to iterate through each key-value pair in the object. If the value of a key is an array or object, we can use a nested for…in loop to iterate through its key-value pairs.

Here’s another example of how to iterate through a nested object:

const nestedObject = {
   name: "Vishal Kukreja",
   age: 32,
   address: {
     street: "123 Mumbai fashion St",
     city: "Midtown",
     state: "MH",
   },
   hobbies: ["reading", "drawing", "gardening"],
 };
 
 for (let key in nestedObject) {
   if (typeof nestedObject[key] === "object") {
     for (let nestedKey in nestedObject[key]) {
       console.log(nestedObject[key][nestedKey]);
     }
   } else {
     console.log(nestedObject[key]);
   }
 }

//Output:
Vishal Kukreja
32
123 Mumbai fashion St
Midtown
MH
reading
drawing
gardening

In this example, we are using a for…in loop to iterate through each key-value pair in the nested object. If the value of a key is an object, we use a nested for…in loop to iterate through its key-value pairs.

Like accessing nested Arrays and Objects, there is an article for updation. Checkout article with e.g. here JavaScript : Find and update a value in an array of objects.


I hope you enjoyed this post. If you have any question/suggestion, feel free to leave your comments below.

For more details on Arrays see MDN Reference.


Dear reader, a sincere review from you would encourage me to write even more 😉

Stay tuned. Happy Learning.

4 10 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x