Js forEach, jsforeach
Zookeeper
The forEach () function traverses the array from start to end. There are three parameters: array element, element index, and array (if a parameter is an array element, it is the value of the array.
Var data = [1, 2, 3, 4, 5, 6];
Var sum = 0;
Data. forEach (function (v) {// where v is the value of the array 123456
Sum + = v ;})
Document. write (sum + "<br>"); // print it as 21
Data. forEach (function (o, p, q) {// corresponds to the array elements, element indexes, and the array itself.
Q [p] = o + 1;
})
Document. write (data );
Note: forEach cannot terminate before all elements are passed to the called function (while the for loop has a break method). To terminate the function in advance, you must put forEach in the try block, and can throw an exception. If a forEach. break exception is thrown by a function called by foreach (), the loop is terminated in advance:
Function foreach (a, B, c ){
Try {
A. forEach (B, c );
} Catch (e ){
If (e = foreach. break) return;
Else throw e;
}
}
Foreach. break = new Error ("StopIteration ");
}