Iterate JSON Object using Object.keys() and Array forEach Method

You can use Object.keys() to get an array of the keys of the JSON object and then iterate over this array using the forEach method. Object.keys(obj) returns an array of the keys of the JSON Object (obj). The forEach method is used to iterate over this array of keys. Inside the forEach loop, you can access the value of each property using obj[key].

Example: This code iterates over the keys of the JavaScript object ‘obj’ using Object.keys(), printing each key along with its corresponding value.

JavaScript
const obj = {
    "company": 'w3wiki',
    "contact": '+91-9876543210',
    "city": 'Noida'
};

Object.keys(obj).forEach(key => {
    console.log(`${key}: ${obj[key]}`);
});

Output
company: w3wiki
contact: +91-9876543210
city: Noida

How to Iterate JSON Object in JavaScript?

JSON (JavaScript Object Notation) is a lightweight data-interchange format commonly used for exchanging data between a server and a web application. In JavaScript, JSON objects are similar to JavaScript objects, and iterating over them is a common task. In this article, we’ll explore different approaches to iterate over a JSON object in JavaScript, along with complete code examples and explanations.

Table of Content

  • Using for…in Loop
  • Using Object.keys() and Array forEach Method
  • Using Object.entries() and Array forEach Method
  • Using for…of Loop with Object.entries()

Similar Reads

Iterate JSON Object using for…in Loop

The for…in loop is a simple and basic method to iterate over the properties of a JSON object. The for…in loop iterates over the keys of the JSON Object (obj). Inside the loop, you can access the value of each property using obj[key]. This method allows you to perform operations on both the keys and values of the JSON object....

Iterate JSON Object using Object.keys() and Array forEach Method

You can use Object.keys() to get an array of the keys of the JSON object and then iterate over this array using the forEach method. Object.keys(obj) returns an array of the keys of the JSON Object (obj). The forEach method is used to iterate over this array of keys. Inside the forEach loop, you can access the value of each property using obj[key]....

Iterate JSON Object using Object.entries() and Array forEach Method

If you need both the keys and values of the JSON object, you can use Object.entries() to get an array of [key, value] pairs and then iterate over this array. Object.entries(obj) returns an array of [key, value] pairs from the JSON Object (obj). The forEach method is used to iterate over this array of entries. Inside the forEach loop, you can directly access both the key and value of each property....

Iterate JSON Object using for…of Loop with Object.entries() Method

You can also use the for…of loop in combination with Object.entries() to iterate over the [key, value] pairs of the JSON object. Object.entries(obj) returns an array of [key, value] pairs from the JSON Object (obj). The for…of loop iterates over this array of entries. Inside the loop, you can directly access both the key and value of each property....

Contact Us