The MongoDB database foreach statement circular traversal function is a very common function.
A foreach loop is used to iterate, and the callback function is allowed to execute once per loop.
In addition, the Foreach Loop traversal is an extension of the For loop, which is consistent with the browser-side foreach usage.
Examples are as follows:
>var arr = ["AB", "CD", "EF"]
>var show = function (Value,index,ar) {print (value)}
>arr.foreach (show)
AB
cd
EF
Additional--A foreach example on the browser side:
Value is the current element values, index is the current element in the array of indices, AR 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 for the callback function, scope sets the context for the callback so that this point of the callback function points to the scope variable, and the scope is optional
Letters.foreach (showresults,scope);