我正在使用mes.js和MongoDB构建一个应用程序,我有一个关于cursor.forEach()的问题. 我希望在每次forEach迭代开始时判断一些条件,如果不需要对元素执行操作,则跳过该元素,这样可以节省一些时间.
这是我的代码:
// Fetch all objects in SomeElements collection
var elementsCollection = SomeElements.find();
elementsCollection.forEach(function(element){
if (element.shouldBeProcessed == false){
// Here I would like to continue to the next element if this one
// doesn't have to be processed
}else{
// This part should be avoided if not neccessary
doSomeLengthyOperation();
}
});
我知道我可以使用游标将游标转换为array.查找().fetch()然后使用regular for循环遍历元素,并正常使用continue和break,但我对forEach()中是否有类似的东西感兴趣.