Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Iterate over properties of an object with ES2017 Object.entries()


    The Object.entries() function is an addition to the ECMAscript scpec in Es2017. This allows us to iterate through the properties of an object and read the entries as keys and objects.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: Let's take a look at the entries object introduced in es2017. We'll make a variable world and give it some properties, continents, and oceans in this case. Now, let's make a variable called entries and capture the result of the Object.entries function into our variable.

    Now that we've captured the result, let's log this and check the result. If you look at the result carefully, you can see that you get a list of objects as a result. Each object is a list in itself and has two objects inside the list.

    The first one is the key, while the second one is the actual object, and it exactly matches the properties and values inside our world object. Now, let's take a for loop and log each entry object from the entries list. Let's take the console log statement from the bottom, cut it, and paste it into our for loop.

    Now, we'll change the entries to entry, because we want to log each entry. Now if you run this, you can see that the output is zero and one. That is because, entries is an iterable object. To get each object out of an iterable, we need to use the for-of loop.

    If we change this to of and run, you can see that we get our list of keys and objects. Now, let's pull out our key and object as separate variables using the array notation. For now, let's log only the keys and check the result. You can see that the keys match exactly each property in our world object.

    Let's also log each of our object just after our key and check the result. As expected, it prints out our key and object in each line. Let's see, if we can also capture a function that is attached to our object. We'll make a simple function and let's call it makepiece, and inside this function, we'll console log piece.

    Now, let's run our index file and check the result. You can see that we have makepiece as a key and the function as an object.