How to useObject.Values() in Javascript
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); }); |
Output
name :John age :30 name :Jane age :25 name :Bob age :40
How to loop through a plain object with the objects as members in JavaScript ?
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()