const - JS | lectureData Structures, Modern Operators & Strings

Looping Objects - Object Keys, Values and Entries

Data Structures, Modern Operators & Strings

We learned about the for-of loop to loop over arrays, which remember is an Iterable, but we can also loop over objects, which are not Iterable, but indirectly. We have different options here, depending on what we want to loop over. Do we want to loop over the object's property names, over the values, or both together? Let's start by simply looping over property names, also known as keys.

Ultimately, we will still have to use the for-of loop to loop over the array, but again, we will do that indirectly. So we're not looping over the object itself. Instead, we're going to loop over an array. Let me show you how and then explain.

script.js
for (const day of Object.keys(openingHours)) {
  console.log(day);
}

OK - 1

We get Thursday, Friday, and Saturday, which are precisely the three key names of the openingHours object. Let's now take a closer look at this mysterious Object.keys.

Let's start by logging what's returned by the Object.keys function.

script.js
const keys = Object.keys(openingHours);
console.log(properties);

OK -2

We get an array with the three property names which, we can use to compute how many properties are in an object. For exalole, let's say we wanted to print a string saying how many days the restaurant is open. That will be quite simple right?

script.js
const keys = Object.keys(openingHours);
console.log(`We are open ${keys.length} days a week.`);

OK -3

We can now change Object.keys in our for-of loop with our variable keys and do something cool 😎

script.js
const keys = Object.keys(openingHours);
console.log(`We are open ${keys.length} days a week.`);
for (const day of keys) {
  console.log(`We are open on: ${day}`);
}

OK -4

Now, what if we actually wanted property values themselves? Well, then we would use Object.values.

script.js
const values = Object.values(openingHours);
console.log(values);

OK -5

We get an array containing three objects which are the values of the keys thu, fri and sat respectively. There is no need to loop over them again because it will work the same as with keys.

To loop over the entire object, we need the entries. Entries is basically keys plus the values together. We already saw the entries before where we had this:

script.js
for (const [index, element] of menu.entries()) {
  console.log(`${index + 1} - ${element}`);
}

We called the entries method on the menu array, which returned the index number and the element itself. Hence, we can do something similar on objects, and that will then return the key and the value. It works differently on objects because it will not be a method that we call on the object itself as we did with the menu array above.

script.js
const entries = Object.entries(openingHours);
console.log(entries);

OK -6

As you might've expected, we now get an array. We can the use this array to loop over the entire object.

script.js
for (const objArr of entries) {
  console.log(objArr);
}

OK -7

Now as you can see in each array, the first element is the key and the second element is the value. To finish, let's print a nice string to the console using destructuring.

script.js
for (const [key, { open, close }] of entries) {
  console.log(`On ${key} we open at ${open} and close at ${close}`);
}

OK -8