“continue” in cursor.forEach()

前端 未结 5 599
猫巷女王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:43

    In my opinion the best approach to achieve this by using the filter method as it's meaningless to return in a forEach block; for an example on your snippet:

    // Fetch all objects in SomeElements collection
    var elementsCollection = SomeElements.find();
    elementsCollection
    .filter(function(element) {
      return element.shouldBeProcessed;
    })
    .forEach(function(element){
      doSomeLengthyOperation();
    });
    

    This will narrow down your elementsCollection and just keep the filtred elements that should be processed.

提交回复
热议问题