How to use Object.values() method and forEach() Method In Javascript

The Object.values() method returns an array of the object’s own enumerable property values. By combining this with the forEach() method, you can iterate over each value. To also access the corresponding keys, you can use the Object.keys() method in tandem.

Syntax:

Object.values(exampleObj).forEach((value, index) => {
const key = Object.keys(exampleObj)[index];
console.log(key, value);
});

Example:

JavaScript
function iterateObject() {
    let exampleObj = {
        book: "Sherlock Holmes",
        author: "Arthur Conan Doyle",
        genre: "Mystery"
    };

    Object.values(exampleObj).forEach((value, index) => {
        const key = Object.keys(exampleObj)[index];
        console.log(key, value);
    });
}

iterateObject();

Output
book Sherlock Holmes
author Arthur Conan Doyle
genre Mystery


How to iterate over a JavaScript object ?

In this article, we will learn how to iterate over a JavaScript object. During iteration, you loop through the object’s properties one by one, and depending on the method you use for iteration, you might have different access to these properties

There are many methods to iterate over an object which are discussed below: 

Table of Content

  • Using for…in loop
  • Using Object.entries() method and map() Method
  • Using forEach() method and object.keys() Method
  • Using Lodash _.forOwn() Method

Similar Reads

Method 1: Using for…in loop

The properties of the object can be iterated over using a for..in loop. This loop is used to iterate over all non-Symbol iterable properties of an object. Some objects may contain properties that may be inherited from their prototypes. The hasOwnProperty() method can be used to check if the property belongs to the object itself. The value of each key of the object can be found by using the key as the index of the object....

Method 2: Using Object.entries() method and map() Method

The Object.entries() method is used to return an array of the object’s own enumerable string-keyed property pairs. The returned array is used with the map() method to extract the key and value from the pairs. The key and values from the key-value pair can be extracted by accessing the first and second index of the array pair. The first index corresponds to the key and the second index corresponds to the value of the pair....

Method 3: Using forEach() method and object.keys() Method

Object.keys() returns an array of keys of the object and forEach() is an array method that allows you to iterate over each element in the array....

Method 4: Using Lodash _.forOwn() Method

In this approach, we are using Lodash _.forOwn() method, which helps to iterate through objects....

Method 5: Using Object.values() method and forEach() Method

The Object.values() method returns an array of the object’s own enumerable property values. By combining this with the forEach() method, you can iterate over each value. To also access the corresponding keys, you can use the Object.keys() method in tandem....