Javascript how to iterate through an object?

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

Listen

Introduction

When working with JavaScript, it is often necessary to iterate through an object to access and manipulate its properties. In this article, we will explore different methods to iterate through an object in JavaScript, providing examples and explanations along the way.

Using a for…in loop

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

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

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

In this example, we use the for…in loop to iterate through the properties of the `person` object. The loop assigns each property key to the variable `key`, which we can then use to access the corresponding value using bracket notation (`person[key]`). This allows us to log each property and its value to the console.

Using Object.keys()

Another approach to iterate through an object is by using the `Object.keys()` method. This method returns an array of a given object’s own enumerable property names. We can then loop over this array using a for…of loop. Here’s an example:

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

const keys = Object.keys(person);

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

In this example, we use `Object.keys()` to get an array of the `person` object’s property names. We then iterate over this array using a for…of loop, similar to how we would iterate over any other array. This allows us to access each property’s value using bracket notation.

Using Object.entries()

The `Object.entries()` method is another useful way to iterate through an object in JavaScript. This method returns an array of a given object’s own enumerable property key-value pairs. We can then loop over this array using a for…of loop. Here’s an example:

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

const entries = Object.entries(person);

for (let [key, value] of entries) {
console.log(key + ‘: ‘ + value);
}
“`

In this example, we use `Object.entries()` to get an array of the `person` object’s key-value pairs. We then iterate over this array using a for…of loop, destructuring each entry into separate `key` and `value` variables. This allows us to access and log both the property key and its corresponding value.

Conclusion

Iterating through an object in JavaScript is a common task that can be accomplished using various methods. The for…in loop, `Object.keys()`, and `Object.entries()` are all effective ways to achieve this. Depending on your specific use case, you can choose the method that best suits your needs.

References

– developer.mozilla.org: [for…in loop](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)