The MongoDB database forEach loops through the usage

  • 2020-05-09 19:32:03
  • OfStack

The MongoDB database forEach statement loop traversal function is a very common one.
The loop is traversed by foreach, allowing the callback function to be executed once per loop.
In addition, the foreach loop traversal is an extension of the for loop, which is 1 compared to the forEach usage on the browser side.

Here's an example:


>var arr = ["ab","cd","ef"]
>var show = function(value,index,ar){ print(value) }
>arr.forEach(show)
ab
cd
ef

Add-on - browser-side forEach example:


//value Is the current element value, index Is the index of the current element in the array, ar Is the array itself 
functionShowResults(value, index, ar) {
document.write("value:" + value);
document.write("index: " + index);
document.write("name: " + this.name);
document.write("
");
}
varletters = ['ab', 'cd', 'ef'];
varscope = { name: 'scope' };
// ShowResults Is the callback function, scope Sets the context for the callback to make the callback function this Point to the scope Variables, scope Is an optional parameter 
letters.forEach(ShowResults,scope);

Related articles: