Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax:
3.2.
Parameters:
3.3.
Return Value:
4.
Examples 
4.1.
Iterating Over Array Elements:
4.2.
JavaScript
4.3.
Processing Object Properties:
4.4.
JavaScript
4.5.
Breaking the Loop:
4.6.
JavaScript
4.7.
Iteration Over a String:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
How does _.forEach() differ from native JavaScript forEach?
5.2.
Can _.forEach() modify the original collection?
5.3.
Is _.forEach() efficient for large collections?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.forEach() Method

Author Riya Singh
0 upvote

Introduction

Iteration over collections is a fundamental aspect of JavaScript programming. Lodash enhances this capability with the _.forEach() method, providing a concise and efficient way to iterate over arrays, objects, and even strings. 

Lodash _.forEach() Method

This article will provide an in-depth look at _.forEach(), demonstrating its versatility and practicality through examples and FAQs.

Why This Function is Used

The _.forEach() function is used for iterating over elements in a collection, such as arrays or objects, and executing a provided function on each element. It's an essential tool for scenarios where each item in a collection requires processing, validation, or manipulation. This method is favored for its readability and flexibility, offering a more declarative approach to iteration compared to traditional for loops.

Syntax, Parameter and Return Value

Syntax:

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

Parameters:

  • collection (Array|Object): The collection to iterate over.
     
  • [iteratee=_.identity] (Function): The function invoked per iteration.

Return Value:

 Returns the original collection.

Examples 

Iterating Over Array Elements:

  • JavaScript

JavaScript

var _ = require('lodash');

var numbers = [1, 2, 3];

_.forEach(numbers, value => console.log(value * 2));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 2, 4, 6


Demonstrates basic iteration over an array with a function applied to each element.

Processing Object Properties:

  • JavaScript

JavaScript

var user = { 'name': 'John', 'age': 30 };

_.forEach(user, (value, key) => console.log(key, value));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

'name' John, 'age' 30


Shows how to iterate over the properties of an object.

Breaking the Loop:

  • JavaScript

JavaScript

_.forEach(numbers, (value) => {

 if (value === 2) return false; // Breaks the loop

 console.log(value);

});
You can also try this code with Online Javascript Compiler
Run Code

Output: 

1


Illustrates stopping iteration based on a condition.

Iteration Over a String:

  • JavaScript

JavaScript

var string = 'hello';

_.forEach(string, char => console.log(char.toUpperCase()));
You can also try this code with Online Javascript Compiler
Run Code

 Output: 

H, E, L, L, O


An example of iterating over each character in a string.

Frequently Asked Questions 

How does _.forEach() differ from native JavaScript forEach?

Lodash's _.forEach() works with a wider range of collections, including objects and strings, and allows for early exit from iteration by returning false.

Can _.forEach() modify the original collection?

Yes, if the iteratee function modifies the elements, the changes will reflect in the original collection.

Is _.forEach() efficient for large collections?

_.forEach() is generally efficient for large collections, but performance may vary based on the complexity of the iteratee function.

Conclusion

Lodash's _.forEach() method is a versatile tool for iterating over and processing elements in various types of collections. It provides an intuitive and flexible approach to iteration, enhancing code readability and maintainability, especially when dealing with complex collections.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass