Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop lodash.js _.each loop?

I have this rows code:

_.each($scope.inspectionReviews, function (value, key) {
    alert("status=" + value.IsNormal + "   " + "name=" + value.InspectionItemName);
    if (!value.IsNormal) {
        $scope.status = false;
        return;
    }
    $scope.status = true;
})

At some point I want to stop looping but it seems that return not working.

How can I stop the loop?

like image 240
Michael Avatar asked Oct 21 '15 18:10

Michael


People also ask

How do you break a forEach loop Lodash?

Javascript. Conclusion: Hence to break Lodash forEach loop we have to return false from the callback function.

Is Lodash still maintained?

Lodash is one of those handful of libraries, that were created to solve a growing problem, that fortunately due to Microsoft's foresight, no longer exists.

What is Lodash JS used for?

Summary. Lodash is a JavaScript library that helps programmers write more concise and maintainable JavaScript. It can be broken down into several main areas: Utilities - for simplifying common programming tasks such as determining type as well as simplifying math operations.

Is Lodash efficient?

Lodash is extremely well-optimized as far as modern JS goes, but, as you may know, nothing can be faster than native implementation. Even if Lodash uses the same API under-the-hood, function call overhead is still present. Some might say that these are just some micro-optimizations.


2 Answers

return false;

Use this in a lodash each to break.

EDIT: I have seen the title changed to underscore. Is it underscore or lodash? As I pointed out above you can break an each in lodash but underscore I believe emulates forEach which natively doesn't provide that.

like image 159
AtheistP3ace Avatar answered Oct 23 '22 20:10

AtheistP3ace


  • return false => it has exactly the same effect as using break;

  • return => this is the same as using continue;

like image 38
jack wu Avatar answered Oct 23 '22 21:10

jack wu