Sometimes, we want to iterate over values of object with JavaScript.
In this article, we’ll look at how to iterate over values of object with JavaScript.
How to iterate over values of object with JavaScript?
To iterate over values of object with JavaScript, we use the Object.entries
method.
For instance, we write
const map = { key1: "value1", key2: "value2" };
for (const [key, value] of Object.entries(map)) {
console.log(`${key}: ${value}`);
}
to use a for-of loop to loop through the array entries returned by Object.entries
.
Object.entries
returns an array of array of key-value pair strings.
We get the key with key
and value from value
.
Conclusion
To iterate over values of object with JavaScript, we use the Object.entries
method.