How we can use forEach with Map and Set In JavaScript

Introduction

A function can be run on each element of an array or object that resembles an array by iterating through the elements using the forEach() method, a built-in function in JavaScript. ForEach() may also be used with the Map and Set data structures in JavaScript, so it isn't limited to arrays.

forEach with Map in JavaScript

When you use the function forEach() with a Map, the value and the element's key are passed as two parameters to the function as an argument. For each key-value pair, the function is run using the forEach() method, which loops across the Map object.

// forEach With Map
const Namelist = new Map([
    ['first', 'Ishika'],
    ['second', 'Shubham'],
    ['third', 'Mishra'],
  ]);
  console.log(Namelist);
  Namelist.forEach(function (value, key, map) {
    console.log(`${key}: ${value}`);
  });

Output

forEach with Set in JavaScript

When using a Set with forEach(), the function you give as an argument only accepts one parameter: the element's value. The function is run each time the forEach() method iterates over a unique value in the Set object.

//forEach With Set
  const Namelist_v = new Set(['Ishika', 'Shubham', 'Mishra', 'Shubham', 'Ishika']);
  console.log(Namelist_v);
  Namelist_v.forEach(function (value, _, map) {
    console.log(`${value}: ${value}`);
  });

Output

Conclusion

The best approach to iterate over the components of these collections in JavaScript is to use forEach() with Map and Set. Your code becomes easier to understand, more readable, and serves an improved purpose.

If you have any queries/suggestions on the article, please leave your questions and thoughts in the comment section below. Follow C# Corner to learn more new and amazing things about JavaScript or to explore more technologies.

Thanks for reading, and I hope you like it.

FAQs

Q1- What is forEach in JavaScript?

Ans- forEach is a built-in JavaScript method that allows you to iterate over an array and execute a callback function for each element.

Q2- How can we use forEach with Maps?

Ans- Maps are not arrays, so they don't have a forEach method. However, you can use the forEach method of the Map object to iterate over the entries of the Map. Here's an example.

const myMap = new Map([['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]);

myMap.forEach((value, key) => {
  console.log(`${key} = ${value}`);
});

Q3- How can we use forEach with Sets?

Ans- Similar to Maps, Sets don't have a forEach method. However, you can use the forEach method of the Set object to iterate over the elements of the Set. Here's an example.

const mySet = new Set(['value1', 'value2', 'value3']);

mySet.forEach((value) => {
  console.log(value);
});

Q4- What is the callback function in forEach?

Ans- The callback function is a function that is executed for each element in the array, Map, or Set. It takes three parameters: the current element, the index of the current element (optional), and the array, Map, or Set being iterated over (optional).

Q5- Can we break out of a forEach loop?

Ans- No, you cannot break out of a forEach loop. If you need to break out of a loop, use a for loop or a while loop instead.