mongodb 在 cursor.forEach() 中“继续”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18452920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"continue" in cursor.forEach()
提问by Drag0
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 then skip the element if I don't have to do the operation on it so I can save some time.
我正在使用meteor.js 和MongoDB 构建一个应用程序,我有一个关于cursor.forEach() 的问题。我想在每次 forEach 迭代开始时检查一些条件,然后如果我不必对其进行操作则跳过该元素,这样我就可以节省一些时间。
Here is my code:
这是我的代码:
// 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();
}
});
I know I could turn cursor to array using cursor.find().fetch() and then use regular for-loop to iterate over elements and use continue and break normally but I'm interested if there is something similiar to use in forEach().
我知道我可以使用 cursor.find().fetch() 将光标转换为数组,然后使用常规的 for 循环来迭代元素并正常使用 continue 和 break 但我很感兴趣是否有类似的东西在 forEach( )。
回答by nnnnnn
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:
的每次迭代forEach()
都会调用您提供的函数。要在任何给定的迭代中停止进一步处理(并继续下一项),您只需return
在适当的点从函数开始:
elementsCollection.forEach(function(element){
if (!element.shouldBeProcessed)
return; // stop processing this iteration
// This part will be avoided if not neccessary
doSomeLengthyOperation();
});
回答by Ramy Tamer
In my opinion the best approach to achieve this by using the filter
methodas it's meaningless to return in a forEach
block; for an example on your snippet:
在我看来,通过使用来实现这一目标的最佳途径filter
方法,因为它是没有意义的返回forEach
块; 以您的代码段为例:
// 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.
这将缩小您的范围elementsCollection
,只保留filtred
应该处理的元素。
回答by jwerre
Here is a solution using for of
and continue
instead of forEach
:
这是一个使用for of
andcontinue
而不是 的解决方案forEach
:
let elementsCollection = SomeElements.find();
for (let el of elementsCollection) {
// continue will exit out of the current
// iteration and continue on to the next
if (!el.shouldBeProcessed){
continue;
}
doSomeLengthyOperation();
});
This may be a bit more useful if you need to use asynchronous functions inside your loop which do not work inside forEach
. For example:
如果您需要在循环中使用异步函数,而这些函数在forEach
. 例如:
(async fuction(){
for (let el of elementsCollection) {
if (!el.shouldBeProcessed){
continue;
}
let res;
try {
res = await doSomeLengthyAsyncOperation();
} catch (err) {
return Promise.reject(err)
}
});
})()
回答by JSON C11
Making use of JavaScripts short-circuitevaluation. If el.shouldBeProcessed
returns true, doSomeLengthyOperation
利用 JavaScript进行短路评估。如果el.shouldBeProcessed
返回真,doSomeLengthyOperation
elementsCollection.forEach( el =>
el.shouldBeProcessed && doSomeLengthyOperation()
);