How to iterate an ES6 Map, and other Javascript objects

How to iterate an ES6 Map, and other Javascript objects

TLDR;

  • With an ES6 Map, use for (const [key, value] of myMap), this is equivalent to for (const [key, value] of myMap.entries()).
  • With an ES6 Set, use for (const value of mySet), this is equivalent to for (const value of mySet.values()).
  • With an array, use for (const value of myArray), this is equivalent to for (const value of myArray.values()).
  • With an ordinary object, use for (const [key, value] of Object.entries(obj)).

Other notes:

Buy Me A Coffee