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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:



0 1

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

0 1

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

0 1

Here the _.some() function returns false when the value 30 is detected. Otherwise the count (cnt) is incremented by 1.

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.