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.