Categories
JavaScript Answers

How to use JavaScript foreach loop on an associative array object?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *