How to Iterate JavaScript Object Containing Array and Nested Objects ?
Last Updated :
05 Feb, 2024
JavaScript provides us with several built-in methods through which one can iterate over the array and nested objects using the for...in loop, Object.keys(), Object.entries(), and Object.values(). Each method serves a distinct purpose in iterating over object properties, keys, and values which are explained as follows:
Using For...in the loop
The for...in loop iterates over the enumerable properties of an object, including those inherited from its prototype chain. It's commonly used for iterating over object properties and performing operations based on each property's value or key.
Example: Iterating through a JavaScript object with arrays and nested objects using built-in methods.
JavaScript
let course = {
title: "Java",
price: 30000,
Instructors: ["Kamal Sir", "Anuv Sir"],
Materials: {
book1: "Effective Java",
book2: "Head First Java",
},
};
for (let key in course) {
if (course.hasOwnProperty(key)) {
console.log(key, course[key]);
}
}
Outputtitle Java
price 30000
Instructors [ 'Kamal Sir', 'Anuv Sir' ]
Materials { book1: 'Effective Java', book2: 'Head First Java' }
Using Object.keys() method
The Object.keys() method returns an array of a given object's enumerable property names. This method provides a way to access the keys of an object, which can be useful for iterating over its properties or performing operations based on its keys. It returns an array containing the strings that represent the names of the object's enumerable properties.
Example: Logging Key-Value Pairs of a JavaScript Object 'course' and its Nested Objects Using Object.keys() and forEach() Loop.
JavaScript
let course = {
title: "Java",
price: 30000,
Instructors: ["Kamal Sir", "Anuv Sir"],
Materials: {
book1: "Effective Java",
book2: "Head First Java",
},
};
Object.keys(course).forEach((key) => {
console.log(key, course[key]);
});
Outputtitle Java
price 30000
Instructors [ 'Kamal Sir', 'Anuv Sir' ]
Materials { book1: 'Effective Java', book2: 'Head First Java' }
Using Object.entries() method
The Object.entries() method returns an array of a given object's enumerable string-keyed property [key, value] pairs. This method is useful for tasks such as logging key-value pairs, transforming object data, or performing operations on object properties.
It takes an object as input and returns an array of key-value pairs, where each pair is represented as a two-element array [key, value].
Example: Logging of Key-Value Pairs in a JavaScript Object 'course' and its Nested Objects Using Object.entries() and forEach() Loop.
JavaScript
let course = {
title: "Java",
price: 30000,
Instructors: ["Kamal Sir", "Anuv Sir"],
Materials: {
book1: "Effective Java",
book2: "Head First Java",
},
};
Object.entries(course).forEach(([key, value]) => {
console.log(key, value);
});
Outputtitle Java
price 30000
Instructors [ 'Kamal Sir', 'Anuv Sir' ]
Materials { book1: 'Effective Java', book2: 'Head First Java' }
Using Object.values() method
The Object.values() method returns an array of the property values of an object. This method provides a simple way to access and work with an object's values without needing to iterate over its keys. It only includes properties that are directly defined on the object itself, not those inherited from its prototype chain.
Example: Displaying Values of a JavaScript Object 'course' and its Nested Objects Using Object.values() and forEach() Loop.
JavaScript
let course = {
title: "Java",
price: 30000,
Instructors: ["Kamal Sir", "Anuv Sir"],
Materials: {
book1: "Effective Java",
book2: "Head First Java",
},
};
Object.values(course).forEach((value) => {
console.log(value);
});
OutputJava
30000
[ 'Kamal Sir', 'Anuv Sir' ]
{ book1: 'Effective Java', book2: 'Head First Java' }
Similar Reads
How to Convert an Object into Array of Objects in JavaScript?
Here are the different methods to convert an object into an array of objects in JavaScript1. Using Object.values() methodObject.values() method extracts the property values of an object and returns them as an array, converting the original object into an array of objects.JavaScriptconst a = { java:
3 min read
How to Remove Null Objects from Nested Array of objects in JavaScript ?
Removing null objects from the nested array of objects can be done by iterating over the array, filtering out the null objects at each level, and then applying a recursive approach to remove all the null objects. This makes sure that all the levels of the nested structure are checked and the null ob
6 min read
How to Convert an Array of Objects to Map in JavaScript?
Here are the different methods to convert an array of objects into a Map in JavaScript1. Using new Map() ConstructorThe Map constructor can directly create a Map from an array of key-value pairs. If each object in the array has a specific key-value structure, you can map it accordingly.JavaScriptcon
3 min read
How to Convert Object to Array in JavaScript?
In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such
4 min read
How to Create a Nested Object in JavaScript ?
JavaScript allows us to create objects having the properties of the other objects this process is called as nesting of objects. Nesting helps in handling complex data in a much more structured and organized manner by creating a hierarchical structure. These are the different methods to create nested
4 min read
JavaScript- Convert an Object to JS Array
Objects in JavaScript are the most important data type and form the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data types (Number, String, Boolean, null, undefined, and symbol). Methods to convert the Objects to JavaScript Array:1. Using Obje
3 min read
How to find property values from an array containing multiple objects in JavaScript ?
To find property values from an array containing multiple objects in JavaScript, we have multiple approaches. In this article, we will learn how to find property values from an array containing multiple objects in JavaScript. Several methods can be used to find property values from an array containi
4 min read
How to convert a map to array of objects in JavaScript?
A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods
6 min read
How to iterate over a JavaScript object ?
Iteration involves looping through the object's properties one by one. Depending on the method used, you can access and manipulate different levels of properties efficiently. Here are several methods.There are many methods to iterate over an object which are discussed below: Table of ContentUsing fo
3 min read
JavaScript - Convert an Array to an Object
These are the following ways to convert an array to an Object:1. Using JavaScript Object.assign() method The first approach is using the Object.assign() method. This method copies the values of all enumerable properties from source objects(one or more) to a target object.JavaScriptlet a = [1, 2, 3,
2 min read