First, the definition
foreach (): Iterates through the array, invoking the specified function for each element.
Map (): Passes each element of the called array to the specified function and returns an array that contains the return value of the function.
The passed function is the first parameter of the foreach ()/Map (), which has three parameters: the element of the array + the index of the element + the array itself.
Ii. Summary of differences
1, map speed than foreach faster;
2, MAP returns a new array with the same length as the original array, and does not affect the original array, and foreach does not produce a new array;
3, map because the return is an array so can chain operation, foreach cannot.
recommended use. Map ()
Third, compatibility issues
1. Advanced browsers (including IE9 and above) support map and foreach methods for loop traversal of arrays,IE6~IE8 does not support
Four
Differences between the foreach and map and for methods of arrays