Friday, October 24, 2025
HomeLanguagesJavascriptLodash _.forEach() Method

Lodash _.forEach() Method

The _.forEach() method iterates over elements of collection and invokes iteratee for each element.

Syntax:

_.forEach( collection, [iteratee = _.identity] )

Parameters: This method accepts two parameters as mentioned above and described below:

  • collection: This parameter holds the collection to iterate over.
  • iteratee: It is the function that is invoked per iteration.

Return Value: This method returns the collection.

Note: Here, const _ = require(‘lodash’) is used to import the lodash library into the file. 
 

Example 1:

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Use of _.forEach() method 
_.forEach(['c', 'cpp', 'java','python'], function(value) {
  console.log(value);
});


Output:

'c'
'cpp'
'java'
'python'
['c', 'cpp', 'java','python']

Example 2:  

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Use of _.forEach() method 
_.forEach({ 'x': 1, 'y': 2 }, function(value, key) {
  console.log(key);
});


Output:

'x'
'y'
{ 'x': 1, 'y': 2 }

Note: This will not work in normal JavaScript because it requires the library lodash to be installed. 

Reference: https://lodash.com/docs/4.17.15#forEach

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS