How to usethe object.entries() method in Javascript
The Object.entries() method in JavaScript returns an array consisting of enumerable property [key, value] pairs of the object. we iterate through that array using for..in loop and get only the properties of that object.
Example: This example shows the implementation of the above-explained approach.
const Teachers = [
{
name: 'saritha',
subject: 'Maths'
},
{
name: 'ahim',
subject: 'science'
},
{
name: 'sneha',
subject: 'Social'
}]
for (const entry of Object.entries(Teachers)) {
for (let value in entry[1]) {
console.log(entry[1][value]);
}
}
Output
saritha Maths ahim science sneha Social
How to loop through an array containing multiple objects and access their properties in JavaScript ?
Looping through an array of elements is straightforward because you can access them using their indexes. However, it’s a bit different with objects since they consist of key-value pairs, and you can’t index them like an array.
To loop through an array of objects in JavaScript, you need to use different methods compared to traditional arrays. JavaScript provides methods like Object.keys(), Object.values(), and Object.entries() to help you access keys and values of objects easily. These methods make it simpler to work with objects and iterate through their properties.
There are many ways to loop through the array of objects. Let’s look at them one by one:
Table of Content
- Using for…in loop
- Using forEach() Method
- Using Object.keys() Method
- Using object.values() Method
- Using the object.entries() method
- Using Lodash _.forEach()