How to break forEach() method in Lodash?


We can break out of a forEach() method in Lodash by using the _.breakLoop() method. This method allows us to immediately exit the loop and discontinue the iteration. To use it, we need to pass in the loop variable as an argument to the _.breakLoop() method within the loop's callback function. Let us first see what is LodashJS −

Lodash is a JavaScript library that provides utility functions for common programming tasks. It is a modern JavaScript utility library delivering modularity, performance & extras. It is similar to Underscore.js and provides a wide range of functions for working with arrays, numbers, objects, and strings.

Lodash is easy to use, and the functions can be chained together to perform multiple operations on the same data. It is also highly optimized for performance and is widely used in web development.

Approach

The forEach() method in Lodash does not have a built-in way to “break” or exit early from the loop.

  • However, you can achieve this by using the some() or every() method instead, and returning true or false respectively when you want to exit the loop.

  • Another way is to use _.find() or _.findIndex() method that stops the iteration when the callback function returns true.

  • You can also use the return statement within the callback function passed to forEach() to exit the loop early. For example, you can use the following code to break a forEach() loop when a certain condition is met −

_.forEach(list, function(item) {
   if (condition) {
      return false;
   }
});

It is worth noting that return statement is not a standard way to break forEach in javascript, it’s just a way of exiting the function, it is not designed to break the loop, but it works as a way to stop the loop in this case because forEach uses the return value of the callback as a way to determine whether to continue iterating over the array.

Example

Here is an example of using the _.forEach() method in Lodash to iterate over an array and breaking out of the loop when a certain condition is met −

const _ = require('lodash');
const myArray = [1, 2, 3, 4, 5];
_.forEach(myArray, (value, index) => {
   if (value === 3) {
      console.log('Found 3 at index', index);
      return false; // this will break out of the loop
   }
   console.log(value);
});

Explanation

In this example, the _.forEach() method is used to iterate over the myArray array. The function passed as the second argument to _.forEach() is a callback that is called for each element in the array. The callback takes two arguments − the current value and the current index.

The callback function checks if the current value is equal to 3. If it is, it logs a message to the console and returns false. This returns false will break out of the loop and the iteration will stop.

If the current value is not equal to 3, the callback function logs the current value to the console.

It will print −

1
2
Found 3 at index 2

Note − _.forEach method does not return any value, so it does not have a return value that can be used to break out of the loop. Instead, _.forEach uses the return value of the callback to determine if the loop should be stopped. Returning false from the callback will stop the loop, while returning any other value will not affect the loop.

Output

Updated on: 16-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements