Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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 forEach iteration and then skip the element if I don't have to do the operation on it so I can save some 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 neccessary  

   doSomeLengthyOperation(); 

  } 

});

I know I could turn cursor to an 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 similar to use in forEach().

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code for “continue” in cursor.forEach():-

elementsCollection.forEach(function(element){

 if (!element.shouldBeProcessed) 

return; 

doSomeLengthyOperation(); 

});

Related questions

0 votes
1 answer
asked Feb 19, 2020 in Web Technology by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 3, 2019 in Web Technology by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...