EN
JavaScript - iterate over object
3 points
In this article, we would like to show you how to iterate over an object in JavaScript.
Below examples show two ways of how to do that:
- Using
for...in
statement - using
Object.entries()
method
The below example shows how to iterate over object
properties using for...in
statement.
Practical example:
xxxxxxxxxx
1
const object = {'a': 1, 'b': 2, 'c': 3};
2
3
for (const prop in object) {
4
console.log(prop);
5
}
The below example shows how to iterate over object
using Object.entries()
to get every key / value pair from object
.
xxxxxxxxxx
1
const object = {'a': 1, 'b': 2, 'c': 3};
2
3
for (const [key, value] of Object.entries(object)) {
4
console.log(`${key}: ${value}`);
5
}