How to break forEach() method in Lodash ?
The Lodash _.forEach() method iterates over elements of the collection and invokes iterate for each element. In this article, we will see how to break the forEach loop in ladash library.
_.forEach( collection, [iterate = _.identity] )
Parameters: This method accepts two parameters as mentioned above and described below:
- collection: This parameter holds the collection to iterate over.
- iterate: It is the function that is invoked per iteration.
Problem: To break forEach loop in Lodash break keyword won’t work. If we do so we get a SyntaxError.
SyntaxError: Illegal break statement
Solution: So from this we know we can’t use break statements as they are not valid in Lodash syntax. So we have to return false from the callback function if we have to break the loop.
Conclusion: Hence to break Lodash forEach loop we have to return false from the callback function.
My Personal Notes arrow_drop_up
Please Login to comment...