Syntax: both foreach and map support 2 parameters: One is the callback function (Item,index,input) and the context;. foreach: Used to iterate through each item in the array; This method executes without a return value and has no effect on the original array; there are several items in the array, So the anonymous callback function that is passed in will need to be executed several times; each time an anonymous function is executed, it is passed three parameter values: The current item in the array item, the index of the current item, and the original array, input; In theory, this method does not return a value, but simply iterates through each item in the array. Do not modify the original array, but we can modify the original array by the index of the array; the this default in the anonymous callback function in the ForEach method is window; therefore: [].foreach (function (Value,index,array) { Code something}), equivalent to: $.each ([],function (Index,value,array) {//code something}) will find that the 1th and 2nd arguments are exactly the opposite, and be careful not to remember them correctly. Similar to the following methods, such as $.map. var arr = [1,2,3,4]; var res= arr.foreach (function (Value,index,array) {Array[index] = value*4;}); Console.log (arr); The result is [4,8,12,16]console.log (res); Undefinedmap:map is the meaning of "mapping" similar to the use of ForEach, which is: [].map (function (Value,index,array) {//code}) var ary = [12,23,24,42,1]; var res = Ary.map (function (item,index,input) {return item*10;}) Console.log (res);//-->[120,230,240,420,10]; Console.log (ary);//-->[12,23,24,42,1]?map: Very similar to foreach, which is used to iterate through each of the values in the array, to iterate over each item in the array; difference: The return value is supported in the callback function of map The return is the equivalent of changing the item in the array (without affecting the originalArray, which is just equivalent to cloning a copy of the original array, the clone of this copy of the array of the corresponding item changed);? Whether a foreach or a map supports the second parameter value, the second parameter means to modify this in the anonymous callback function.
foreach, map function
in JavaScript