VOOZH about

URL: https://www.geeksforgeeks.org/javascript/lodash-_-foreach-method/

⇱ Lodash _.forEach() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lodash _.forEach() Method

Last Updated : 3 Sep, 2024

Lodash _.forEach() method iterates over elements of the collection and invokes iterate for each element.

Syntax:

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

Parameters:

  • 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. 

Example 1: In this example, we are iterating an array by the use of the _.forEach() method and printing the results in the console.

Output:

'c'
'cpp'
'java'
'python'

Example 2: In this example, we are iterating an object by the use of the _.forEach() method and printing the results in the console.

Output:

'x'
'y'

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

Comment