How To Iterate Over A Map In JavaScript
To iterate through the elements in a JavaScript Map()
, you can forEach
. It will call the function you pass as the argument and will pass the value
as the first parameter, and the key
as the second parameter.
const someMap = new Map();
someMap.set("cat", "meow");
someMap.set("dog", "bark");
someMap.set("fish", "gurgle(?)");
someMap.forEach((value, key) => {
console.log({ key, value });
});
Check your browser's console log for the result of this code!