Skip to content
the poor coder

loop on an object

Poor Man
Published
Reading time
2 min read
On this page

Loop on an Object#

Looping over an object is a common task in JavaScript programming. In simple terms, an object is a collection of key-value pairs, and we need to loop over each key-value pair to access its properties. There are multiple ways to loop over an object, and we will discuss some of the most popular ways in this article.

Using a for...in Loop#

The for...in loop is the most popular way to loop over an object in JavaScript. It allows us to iterate over the object's keys and access its values. Here's an example:

Text

    const myObj = { name: "Raju", age: 30, city: "New York" };

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

The output of this code will be:

Text

    name: Raju
    age: 30
    city: New York

As you can see, we have used the for...in loop to iterate over the keys of myObj and access its values using bracket notation.

Using Object.keys() Method#

The Object.keys() method returns an array of the object's keys, and we can use it to loop over the object. Here's an example:

Text

    const myObj = { name: "Raju", age: 30, city: "New York" };
    const keys = Object.keys(myObj);

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

The output of this code will be the same as the previous example.

Using Object.values() Method#

The Object.values() method returns an array of the object's values, and we can use it to loop over the object. Here's an example:

Text

    const myObj = { name: "Raju", age: 30, city: "New York" };
    const values = Object.values(myObj);

    for (let i = 0; i < values.length; i++) {
        console.log(values[i]);
    }

The output of this code will be:

Text

    Raju
    30
    New York

Using Object.entries() Method#

The Object.entries() method returns an array of the object's key-value pairs, and we can use it to loop over the object. Here's an example:

Text

    const myObj = { name: "Raju", age: 30, city: "New York" };
    const entries = Object.entries(myObj);

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

The output of this code will be the same as the previous examples.

So, these are some of the most popular ways to loop over an object in JavaScript. You can choose any of these methods depending on your requirements and coding style.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC