How to iterate object in javascript?

Software
AffiliatePal is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Listen

Introduction

Iterating over objects in JavaScript is a common task that developers encounter frequently. Object iteration allows you to access and manipulate the properties and values of an object. In this article, we will explore different methods and techniques to iterate over objects in JavaScript.

Using for…in loop

One of the most common ways to iterate over an object in JavaScript is by using a for…in loop. This loop iterates over all enumerable properties of an object, including inherited properties. Here’s an example:

“`javascript
const person = {
name: ‘John’,
age: 30,
profession: ‘Developer’
};

for (let key in person) {
console.log(key + ‘: ‘ + person[key]);
}
“`

In the above code, we use the for…in loop to iterate over the properties of the `person` object. The `key` variable represents each property name, and `person[key]` gives us the corresponding value. This allows us to access and manipulate each property of the object.

Using Object.keys()

Another method to iterate over object properties is by using the `Object.keys()` method. This method returns an array of a given object’s own enumerable property names. We can then use a forEach loop or any other array iteration method to iterate over these keys. Here’s an example:

“`javascript
const person = {
name: ‘John’,
age: 30,
profession: ‘Developer’
};

Object.keys(person).forEach(key => {
console.log(key + ‘: ‘ + person[key]);
});
“`

In the above code, we use `Object.keys(person)` to get an array of keys, and then we use `forEach` to iterate over each key. Inside the loop, we access the corresponding value using `person[key]`.

Using Object.entries()

ES8 introduced the `Object.entries()` method, which returns an array of a given object’s own enumerable string-keyed property [key, value] pairs. We can then iterate over these pairs using any array iteration method. Here’s an example:

“`javascript
const person = {
name: ‘John’,
age: 30,
profession: ‘Developer’
};

Object.entries(person).forEach(([key, value]) => {
console.log(key + ‘: ‘ + value);
});
“`

In the above code, `Object.entries(person)` returns an array of [key, value] pairs, and we use `forEach` to iterate over each pair. Inside the loop, we can directly access the key and value using destructuring assignment.

Using Object.getOwnPropertyNames()

The `Object.getOwnPropertyNames()` method returns an array of all properties (enumerable or not) found directly upon a given object. We can use this method to iterate over all properties of an object, including non-enumerable properties. Here’s an example:

“`javascript
const person = {
name: ‘John’,
age: 30,
profession: ‘Developer’
};

Object.getOwnPropertyNames(person).forEach(key => {
console.log(key + ‘: ‘ + person[key]);
});
“`

In the above code, `Object.getOwnPropertyNames(person)` returns an array of all property names, and we use `forEach` to iterate over each key. Inside the loop, we can access the corresponding value using `person[key]`.

Conclusion

Iterating over objects in JavaScript is essential for accessing and manipulating their properties and values. We explored various methods, including the for…in loop, Object.keys(), Object.entries(), and Object.getOwnPropertyNames(). Each method has its own advantages and use cases, so choose the one that best fits your requirements.

References

– developer.mozilla.org – [for…in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for…in)
– developer.mozilla.org – [Object.keys()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)
– developer.mozilla.org – [Object.entries()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries)
– developer.mozilla.org – [Object.getOwnPropertyNames()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames)