How can I use “continue” functionality in the cursor.forEach() method in JavaScript?
I’m building an app using Meteor.js and MongoDB, and I have a question about cursor.forEach(). I want to check some conditions at the beginning of each iteration and skip the current element if I don’t need to perform the operation on it, which would save processing time.
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 necessary
doSomeLengthyOperation();
}
});
I know that I could convert the cursor to an array using cursor.find().fetch() and then use a regular for-loop to iterate over the elements with continue and break, but I’m interested in finding a similar approach to use with forEach(). I’m looking for guidance on how to implement this with javascript foreach continue techniques.