Introduction
In JavaScript, we may wish to loop through an object. In this post, we'll look at how to do it.
How to loop through a JavaScript object?
To loop through a JavaScript object we can use several methods such as: Object.keys(obj)
, for...of
, Object.entries()
or for...of
.
To iterate over object properties we can use Object.keys()
combined with and Array.prototype.forEach()
method
To loop through an Object in JavaScript we can use for...of
introduced in ECMAScript 6
Iterating over an Object's properties could be achieved with Object.entries()
method from ECMAScript 8
The for...of
loop combined with destructuring, and Object.entries
:
There is also an old fashioned way to iterate over an object in JavaScript
Conclusion
In this article, we learned how to loop through a JavaScript object. As you can see there are several methods to do it, you can choose the best that fits your needs.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}