Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to loop through object in JavaScript

In this blog post, we're going to explore how to loop through objects in JavaScript. Whether you're a beginner or an experienced programmer, understanding how to work with objects and loop through their properties is a valuable skill. By the end of this article, you'll have a clear understanding of the different ways you can loop through objects in JavaScript and when to use each approach.

What is an object in JavaScript?

In JavaScript, an object is a collection of properties, where each property is a key-value pair. The key is a unique identifier (usually a string), and the value can be any data type, such as a string, number, array, or even another object. Objects are widely used in JavaScript as a way to store and organize data.

Think of an object as a container holding several labeled compartments. Each compartment has a label (the key) and something inside it (the value). For example, let's consider an object representing a person:

const person = {
  name: 'John Doe',
  age: 30,
  city: 'New York'
};

In this object, there are three properties: name, age, and city, each holding a value.

Looping through an object

Now that we understand what an object is, let's learn how to loop through its properties. Looping through an object means iterating over its properties one by one, usually to perform some operation on the property values or keys. There are several ways to do this in JavaScript, and we'll explore them one by one.

Using the for...in loop

The for...in loop is the most common and straightforward way to loop through an object's properties. It iterates over an object's enumerable properties, which are the properties that can be looped through.

Here's an example of using a for...in loop to loop through the person object we defined earlier:

for (const key in person) {
  console.log(`${key}: ${person[key]}`);
}

This code will output the following:

name: John Doe
age: 30
city: New York

Notice that the loop variable key represents the property name, and the value is accessed using the object's name followed by the key in square brackets (e.g., person[key]). The for...in loop is simple and works well for most use cases.

Using the Object.keys() method

Another way to loop through an object's properties is by using the Object.keys() method. This method takes an object as its argument and returns an array of the object's property names.

Here's an example of using Object.keys() to loop through the person object:

const keys = Object.keys(person);
for (const key of keys) {
  console.log(`${key}: ${person[key]}`);
}

This code will produce the same output as the for...in loop example. Notice that we're using a for...of loop to iterate through the array of keys returned by Object.keys(). This approach can be useful when you need to work with an array of keys, such as when you want to sort the keys before iterating through them.

Using the Object.values() method

If you're only interested in an object's values and don't need the property names, you can use the Object.values() method. This method takes an object as its argument and returns an array of the object's property values.

Here's an example of using Object.values() to loop through the person object:

const values = Object.values(person);
for (const value of values) {
  console.log(value);
}

This code will output the following:

John Doe
30
New York

Notice that we're using a for...of loop to iterate through the array of values returned by Object.values(). This approach can be useful when you don't need the property names and only want to work with the values.

Using the Object.entries() method

The Object.entries() method takes an object as its argument and returns an array of the object's property names and values as pairs. Each pair is an array containing the property name as the first element and the property value as the second element.

Here's an example of using Object.entries() to loop through the person object:

const entries = Object.entries(person);
for (const [key, value] of entries) {
  console.log(`${key}: ${value}`);
}

This code will produce the same output as the for...in loop example. Notice that we're using a for...of loop with array destructuring (const [key, value]) to access the property name and value directly. This approach can be useful when you need to work with both the property names and values in a more structured way.

Conclusion

In this blog post, we've explored different ways to loop through objects in JavaScript:

  • Using the for...in loop for simple and straightforward iteration
  • Using the Object.keys() method to work with an array of property names
  • Using the Object.values() method to work with an array of property values
  • Using the Object.entries() method to work with an array of property name-value pairs

You should now have a solid understanding of how to loop through objects in JavaScript and when to use each approach. Remember that the best method to use depends on your specific use case and what you need to accomplish with the loop. Happy coding!