How to useLodash _.forEach() method in JavaScript

In this example, we are using the Lodash _.forEach() method for iteration of set.

Example: This example shows the implementation of above-explained approach.

Javascript
// Requiring the lodash library 
const _ = require("lodash");
const Data = new Set();

Data.add("Delhi");
Data.add("Noida");
Data.add("Gurgaon");

// Use of _.forEach() method 
_.forEach(Data, function (value) {
    console.log(value);
});

Output:

Delhi
Noida
Gurgaon

How to iterate over Set elements in JavaScript ?

Set() objects are groups of distinct values. A set is a collection of unique elements i.e. no element can appear more than once in a set. Because sets in ES6 are ordered, the items of a set can be iterated in the order of insertion. Object and primitive values can both be stored in sets.

We can iterate over the set elements using the following methods in JavaScript:

Table of Content

  • Approach 1: Using for…of loop
  • Approach 2: Using forEach() Method
  • Approach 3: Using Array.from() and for loop Methods
  • Approach 4: Using Lodash _.forEach() method
  • Approach 5: Using Spread Operator

Similar Reads

Approach 1: Using for…of loop

The for…of loop iterates over the iterable objects (like Array, Map, Set, arguments object, …, etc), invoking a custom iteration hook with statements to be executed for the value of each distinct property....

Approach 2: Using forEach() Method

The arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array....

Approach 3: Using Array.from() and for loop Methods

In this approach, we will use an array.from() method to convert the set into an array and then we iterate using the for loop....

Approach 4: Using Lodash _.forEach() method

In this example, we are using the Lodash _.forEach() method for iteration of set....

Approach 5: Using Spread Operator

The spread operator … can be used to expand elements of an iterable like a Set into an array. This array can then be directly iterated using any array iteration method such as forEach()....