Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to iterate through object in JavaScript

Understanding Objects in JavaScript

Objects in JavaScript represent a collection of properties, and each property is a key-value pair. The key is always a string, and the value can be anything. You can think of an object as a big box that contains smaller boxes (properties). Each smaller box has a label (key) and something inside it (value).

In JavaScript, you can create an object like this:

let person = {
  name: "John Doe",
  age: 25,
  country: "USA"
};

Here, person is an object with three properties: name, age, and country.

Iterating Over Object Properties

Just like you would open all the smaller boxes in a big box one by one, in JavaScript, you can go through all properties (key-value pairs) of an object one by one. This process is called iterating over an object.

There are several ways to iterate through the properties of an object in JavaScript. We will cover the three most common methods: for...in loop, Object.keys(), and Object.entries().

Iterating using for...in loop

The for...in loop is the most straightforward way to iterate over an object's properties.

Here is how you can use it:

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

In the above code, key will be each key in the person object. person[key] will be the value of that key. The console.log() function will print the key and its value to the console.

Iterating using Object.keys()

The Object.keys() method returns an array of an object's keys. You can then iterate over this array to access the values in the object.

Here is how you can use it:

let keys = Object.keys(person);

for (let i = 0; i < keys.length; i++) {
  let key = keys[i];
  console.log(key + ": " + person[key]);
}

In the above code, Object.keys(person) returns an array of keys (['name', 'age', 'country']). The for loop then iterates over this array, and person[key] gives the value of each key.

Iterating using Object.entries()

The Object.entries() method returns an array of an object's key-value pairs. Each key-value pair is an array where the first element is the key and the second element is the value.

Here is how you can use it:

let entries = Object.entries(person);

for (let i = 0; i < entries.length; i++) {
  let entry = entries[i];
  let key = entry[0];
  let value = entry[1];
  console.log(key + ": " + value);
}

In the above code, Object.entries(person) returns an array of key-value pairs ([['name', 'John Doe'], ['age', 25], ['country', 'USA']]). The for loop then iterates over this array, and entry[0] and entry[1] give the key and the value, respectively.

Choosing the Right Method

The for...in loop is the easiest to write and understand, but it has a drawback. It not only iterates over the properties of the object itself, but also over the properties inherited from its prototype (a prototype is like a blueprint that objects use to inherit properties and methods). In most cases, you will only want to iterate over the object's own properties. In such cases, you should use Object.keys() or Object.entries().

Conclusion

Just like a treasure hunt, iterating over an object can lead you to the valuable information you need. Whether you're using a for...in loop, Object.keys(), or Object.entries(), each method has its own unique map to guide you through the object. It's like choosing between a road trip, a train ride, or a flight. Each will get you to your destination, but the journey will be different. So, choose the method that suits your journey best, and happy coding!