How to break _.each() function in Underscore.js ? - GeeksforGeeks

02 Jun, 2020
It is not possible to break the _.each function. The reason is that the _.each function works similarly to the forEach function and imitates its native behavior. It does not allow us to break the loop or escape from it except by throwing an exception. However, one can use different methods like:
Throw Exception
_.find() function
_.some() function
Throw Exception: One can throw an exception from each on getting the desired value.
Syntax:
try {
     _(arrayName).each(function(elementName){
        // your code with condition where 
        // exception is to be thrown
     })
}
catch(exception){
     // dont do anything here
}

Example:
<script>    var arr = [10, 20, 30, 40];    var cnt = 0;    try {        _(arr).each(function (value) {            if (value == 30) {                throw new Error();            }            // Write your own code to            //  use the other values,            // for example:            console.log(cnt++);        })    }    catch (e) {        // Don't do anything here    }</script>
Output:0 1
Output:
Here exception is thrown when the value 30 is detected. Otherwise the count (cnt) is incremented by 1
_.find() Function: The _.find() function can be used to break the loop when the required value is found. The result can be stored in an external variable.
_.find(arayName, function(elementName){
     if(elementName == value){
         return false;
     }
     // Write your own code
}

Example:
<script>    var arr = [10, 20, 30, 40];    var cnt = 0;    _.find(arr, function (value) {        if (value == 30) {            return false;        }          // Write your own code         // to use the other values,        // for example:        console.log(cnt++);    });</script>
Output:0 1
Output:
Here the _.find() function returns false when the value 30 is reached. Otherwise the count (cnt) is incremented by 1
Note: One can also include the finally block after the catch block.
_.some() Function: The _.some() function is similar to the _.find() function and stops traversing the list once the required value is detected. Result can be stored in an external variable.
Syntax:
_.some(arayName, function(elementName){
    if(elementName==value){
        return false;
    }
    
    // Write your own code
}

Example:
<script>    var arr = [10, 20, 30, 40];    var cnt = 0;    _.some(arr, function (value) {        if (value == 30) {            return false;        }          // Write your own code         // to use the other values,        // for example:        console.log(cnt++);    });</script>
Output:0 1
Output:
Here the _.some() function returns false when the value 30 is detected. Otherwise the count (cnt) is incremented by 1.
JavaScript - Underscore.js
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills