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
Recommended Posts:
- JavaScript Array forEach() Method
- ES6 | Array forEach() Method
- TypeScript | Array forEach() Method
- JavaScript Map forEach() Method
- HTML DOM NodeList.forEach() Method
- How to stop forEach() method in JavaScript ?
- Iterate associative array using foreach loop in PHP
- JavaScript | typedArray.forEach() with Examples
- Determine the first and last iteration in a foreach loop in PHP?
- How to remove an array element in a foreach loop?
- Performance of for vs foreach in PHP
- AngularJS | angular.forEach() Function
- Node | URLSearchParams.forEach()
- Node.js | forEach() function
- PHP | foreach Loop
- How to find the index of foreach loop in PHP ?
- Lodash _.method() Method
- Lodash _.sneq() Method
- Lodash _.toQuery() Method
- Lodash _.uniqWith() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


