Categories
JavaScript Answers

How to loop through a JavaScript object array?

Spread the love

Sometimes, we want to loop through a JavaScript object array.

In this article, we’ll look at how to loop through a JavaScript object array.

How to loop through a JavaScript object array?

To loop through a JavaScript object array, we use the for-of loop.

For instance, we write

const messages = [
  {
    msgFrom: "13223821242",
    msgBody: "Hi there",
  },
  {
    msgFrom: "Bill",
    msgBody: "Hello!",
  },
];

for (const message of messages) {
  console.log(message);
}

to loop through the messages array with a for-of loop.

We log the value of the messages entry message in the loop body.

Conclusion

To loop through a JavaScript object array, we use the for-of loop.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *