To use JavaScript foreach loop on an associative array object, we use the Object.entries
method.
For instance, we write
const h = {
a: 1,
b: 2,
};
Object.entries(h).forEach(([key, value]) => console.log(value));
to call Object.entries
with h
to return a key-value pair array.
Then we call forEach
with a callback that destructures the key
and value
from the array.
And we log the property value
of each entry.