How to iterate over a JavaScript object ? Last Updated : 21 Aug, 2024 Comments Improve Suggest changes Like Article Like Report Iteration involves looping through the object's properties one by one. Depending on the method used, you can access and manipulate different levels of properties efficiently. Here are several methods.There are many methods to iterate over an object which are discussed below: Table of ContentUsing for...in Loop Using Object.entries() and map() MethodUsing forEach() and object.keys() MethodMethod 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. Syntax: for (let key in exampleObj) { if (exampleObj.hasOwnProperty(key)) { value = exampleObj[key]; console.log(key, value); } }Example: This example shows the implementation of the above-explained approach. JavaScript function iterateObject() { let exampleObj = { book: "Sherlock Holmes", author: "Arthur Conan Doyle", genre: "Mystery" }; for (let key in exampleObj) { if (exampleObj.hasOwnProperty(key)) { value = exampleObj[key]; console.log(key, value); } } } iterateObject(); Outputbook Sherlock Holmes author Arthur Conan Doyle genre Mystery Method 2: Using Object.entries() and map() MethodThe 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. Syntax: Object.entries(exampleObj).map(entry => { let key = entry[0]; let value = entry[1]; console.log(key, value); });Example: This example shows the implementation of above-explained approach. JavaScript function iterateObject() { let exampleObj = { book: "Sherlock Holmes", author: "Arthur Conan Doyle", genre: "Mystery" }; Object.entries(exampleObj).map(entry => { let key = entry[0]; let value = entry[1]; console.log(key, value); }); } iterateObject(); Outputbook Sherlock Holmes author Arthur Conan Doyle genre Mystery Method 3: Using forEach() and object.keys() Methodobject.keys() Method returns an array of keys of the object and forEach() method is an array method that allows you to iterate over each element in the array.Example: This example shows the implementation of above-explained approach. JavaScript function iterateObject() { let exampleObj = { book: "Sherlock Holmes", author: "Arthur Conan Doyle", genre: "Mystery" }; Object.keys(exampleObj).forEach(key => { const value = exampleObj[key]; console.log(`${key}: ${value}`); }); } iterateObject(); Outputbook: Sherlock Holmes author: Arthur Conan Doyle genre: Mystery Comment More infoAdvertise with us Next Article How to iterate over a JavaScript object ? sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies javascript-object Similar Reads How to create object properties in JavaScript ? JavaScript is built on an object-oriented framework. An object is a collection of properties, where each property links a key to a value. These properties are not in any specific order. The value of a JavaScript property can be a method (function). Object properties can be updated, modified, added, 4 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 declare object with computed property name in JavaScript ? In this article, we learn how to declare an object with a computed property name. Before beginning this article, we have to know about the javascript objects. Computed Property Names: The ES6 computed property names feature allows us to compute an expression as a property name on an object. Javascri 2 min read How to add and remove properties from objects in JavaScript ? We will try to understand how to add properties to an object and how to add or remove properties from an object in JavaScript.Before we go and see the addition and removal of properties from an object let us first understand the basics of an object in JavaScript.Object:An object in JavaScript is a c 6 min read How to check if a value is object-like in JavaScript ? In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope 4 min read JavaScript - Convert Two-Dimensional Array Into an Object Here are the different methods to convert the two-dimensional array into an object in JavaScript.1. Using a for LoopThe simplest way to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs.JavaScriptconst a = [['name', 'Abhay 2 min read How to get all the methods of an object using JavaScript? In this article, we will learn how to get all the methods of an object using JavaScript. In JavaScript, we can get all the methods of an object by iterating over each object and checking if its property value is a function. An HTML document contains some methods and the task is to get all methods of 2 min read How to implement a filter() for Objects in JavaScript ? In this article, we will learn how to implement a filter() for Objects in JavaScript. In JavaScript, the filter() method is a built-in function for the array, not for the object. The filter() method outputs all the elements of an array that pass a specific test or satisfies a specific function. The 3 min read How to add a property to a JavaScript object using a variable as the name? In JavaScript, you can dynamically add a property to an object using a variable as the name. This allows you to create flexible and dynamic objects based on runtime values, enhancing code reusability and adaptability. There are several methods that can be used to add a property to a JavaScript objec 2 min read How to Create Dynamic Values and Objects in JavaScript? In JavaScript, you can choose dynamic values or variable names and object names and choose to edit the variable name in the future without accessing the array, Dynamic values and objects in JavaScript allow changing, accessing, or creating values and object properties at runtime for flexibility and 3 min read Like