Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'continue' inside a each loop : underscore, node.js

People also ask

Is underscore each async?

_. each is synchronous, and will only return after the function was executed on all items. If that. get is asynchronous, each won't help you with that.

How do you continue in each JavaScript?

each() loop at a particular iteration by making the callback function return false . Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration. Note that $(selector). each() and $.


_.each(users, function(u, index) {
  if (u.superUser === false) {
    return;
    //this does not break. _.each will always run
    //the iterator function for the entire array
    //return value from the iterator is ignored
  }
  //Some code
});

Side note that with lodash (not underscore) _.forEach if you DO want to end the "loop" early you can explicitly return false from the iteratee function and lodash will terminate the forEach loop early.


Instead of continue statement in for loop you can use return statement in _.each() in underscore.js it will skip the current iteration only.