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.

Javascript
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()

Similar Reads

Approach 1: Using for…in loop

The properties of the object can be iterated over using a for..in loop. The value of each key of the object can be found by using the key as the index of the object....

Approach 2: Using forEach() Method

The arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array....

Approach 3: Using Object.keys() Method

The Object.keys() method in JavaScript returns an array whose elements are strings corresponding to the enumerable properties. The Object.keys() method returns only the own property names....

Approach 4: Using object.values() Method

The Object.values() method is used to return an array whose elements are the enumerable property values found on the object. we iterate through that array and get only the properties of that object....

Approach 5: Using the object.entries() method

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....

Approach 6: Using Lodash _.forEach()

The _.forEach() method iterates over elements of collection and invokes iteratee for each element....