How to loop through a plain object with the objects as members in JavaScript ?
Last Updated :
20 Jul, 2023
Looping through a plain JavaScript object with objects as members means iterating over each property of the object and accessing its values, which may also be objects. This is a common task in JavaScript, especially when dealing with JSON data or APIs.
There are several ways to loop through a plain JavaScript object with objects as members. Here are four common approaches
- Using For...in loop
- Using Object.keys()
- Using Object.entries()
- Using Object.Values()
This approach uses a for...in loop to iterate over each property of the object and access its values. For each property, the loop sets the key as the name of the property and the value as the value of the property. This approach is useful when you want to perform a similar action for each property of the object.
Syntax:
//for...in loop
for (let key in object) {
// do something with object[key]
}
Example: Using a for...in loop to iterate over an object with objects as members.
JavaScript
let person = {
name: "John",
age: 30,
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345"
}
};
for (let key in person) {
console.log(key + ": " + person[key]);
};
Outputname: John
age: 30
address: [object Object]
This approach uses the Object.keys() method to get an array of all the keys in the object. It then uses the forEach() method to iterate over each key in the array and access its corresponding value. This approach is useful when you want to perform a specific action for each key in the object.
Syntax:
// Object.keys()
Object.keys(object).forEach(function (key) {
// do something with object[key]
});
Example: Using Object.keys() to iterate over an object with objects as members
JavaScript
const myObj = {
prop1: { name: 'John', age: 25 },
prop2: { name: 'Sarah', age: 30 },
prop3: { name: 'Tom', age: 20 }
};
const newArray = Object.keys(myObj).map(key => {
const obj = myObj[key];
return { ...obj, id: key };
// Adding an id property to the object
});
console.log(newArray);
Output[
{ name: 'John', age: 25, id: 'prop1' },
{ name: 'Sarah', age: 30, id: 'prop2' },
{ name: 'Tom', age: 20, id: 'prop3' }
]
This approach uses the Object.entries() method to get an array of all the key-value pairs in the object. It then uses the forEach() method to iterate over each key-value pair and access the value, which could be an object. This approach is useful when you want to perform a specific action for each key-value pair in the object.
Syntax:
// Object.entries()
Object.entries(object).forEach(function([key, value]) {
// do something with value (which could be an object)
});
Example: Using Object.entries() to iterate over an object with objects as members.
JavaScript
let person = {
name: "John",
age: 30,
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345"
}
};
Object.entries(person).forEach(([key, value]) => {
console.log(key + ": " + value);
});
Outputname: John
age: 30
address: [object Object]
In this approach Object.values() returns an array of values from an object. It's used to loop through values, access properties in the person object, and printing names and ages.
Syntax:
Object.values(obj)
Example: Using Object.values(), loop through objects in the person object and log their names and ages.
JavaScript
const person = {
obj1: { name: "John", age: 30 },
obj2: { name: "Jane", age: 25 },
obj3: { name: "Bob", age: 40 }
};
Object.values(person).forEach(obj => {
console.log("name :" + obj.name, "age :" + obj.age);
});
Outputname :John age :30
name :Jane age :25
name :Bob age :40
Similar Reads
How to unflatten an object with the paths for keys in JavaScript ?
In this article, we will learn to unflatten an object which has a certain number of key-value pairs(s) within it with the paths for the keys in JavaScript. Problem Statement: You are given an object with several key-value pair(s). Some keys have various keys names present after a dot, you basically
4 min read
How to Separate Array of Objects into Multiple Objects in JavaScript ?
In JavaScript, the task of separating an array of objects into multiple objects involves organizing and restructuring data based on specific criteria or properties. This process is often necessary when dealing with datasets that need to be grouped or segmented for easier analysis or manipulation. Th
5 min read
How to pass primitive/object types through functions in JavaScript ?
In this article, we learn how JavaScript primitive/object types passed through functions. First, we will see, what are primitive and object data types in JavaScript: 1. Primitive data type: The predefined data types that are provided by JavaScript are called primitive data type. The primitive data t
4 min read
How to Loop through array of JSON object with *ngFor in Angular ?
JavaScript Object Notation (JSON) is a text-based, human-readable interchange format used for representing simple data structures and objects in web browser-based code. In order to Loop through an array of JSON objects, the *ngFor directive can be utilized that helps to dynamically generate HTML con
3 min read
How to create a new object from the specified object, where all the keys are in lowercase in JavaScript?
In this article, we will learn how to make a new object from the specified object where all the keys are in lowercase using JavaScript. Example: Here, we have converted the upper case to lower case key values. Input: {RollNo : 1, Mark : 78} Output: {rollno : 1, mark : 78} Approach 1: A simple approa
2 min read
How to get the Class Name of an Object in JavaScript
In this article, we will learn how we can get the class Name of an Object with multiple approaches in JavaScript. In JavaScript, determining an object's class name can be done using multiple approaches. The constructor property of an object reveals the function that created it, while the instanceof
3 min read
How to create an object with prototype in JavaScript ?
In this article, we will discuss object creation & prototypes, along with understanding the different ways for object creation & their implementation through the examples. Prototypes are the mechanism by which objects in JavaScript inherit features from another object. A prototype property i
4 min read
How to print the content of an object in JavaScript ?
To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data. Let's first create a JavaScript Object containing some key-values as given below: JavaScript // Given Object const obj = { name: 'John', age: 30, city:
3 min read
How to Loop Through an Array in JavaScript?
Here are the various ways to loop through an array in JavaScript1. Using for LoopThe for loop is one of the most used ways to iterate over an array. It gives you complete control over the loop, including access to the array index, which can be useful when you need to modify elements or perform other
4 min read
How to test a string as a literal and as an object in JavaScript ?
In this article, we learn how to test a string as a literal and as an object using JavaScript. What is JavaScript Literal?Literals are ways of representing fixed values in source code. In most programming languages, values are notated by integers, floating-point numbers, strings, and usually by bool
3 min read