The MongoDB database forEach loops through usage

  • 2020-05-07 20:38:20
  • OfStack

The MongoDB database forEach statement loop traversal feature is a very common feature.
The loop is traversed by foreach, allowing one callback function to be executed 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-ons - 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 so that the callback function is this Point to the scope Variables, scope Is an optional parameter 
letters.forEach(ShowResults,scope);

Related articles: