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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read