“continue” in cursor.forEach()

前端 未结 5 598
猫巷女王i
猫巷女王i 2020-12-12 14:04

I\'m building an app using meteor.js and MongoDB and I have a question about cursor.forEach(). I want to check some conditions in the beginning of each forEach iteration and

5条回答
  •  青春惊慌失措
    2020-12-12 14:29

    Each iteration of the forEach() will call the function that you have supplied. To stop further processing within any given iteration (and continue with the next item) you just have to return from the function at the appropriate point:

    elementsCollection.forEach(function(element){
      if (!element.shouldBeProcessed)
        return; // stop processing this iteration
    
      // This part will be avoided if not neccessary
      doSomeLengthyOperation();
    });
    

提交回复
热议问题