map () method:
Array.map (array element, subscript of array element, array itself) =>{}[,thisarray])
1. A loop from 0 to length-1 occurs within the array at the time of invocation;
2. The return value is an array consisting of the return value of each loop call;
3, Thisarray Optional, specify the this in the function, note the arrow function
Test Cases and Results:
1. This is not specified
var arr1 = [' A ',, ' C ', ' d '];arr1.map (function(parameter, index, arr) { Console.log ( '--------------'); Console.log (' parameter: ', parameter); Console.log (' index: ', index); Console.log (' arr: ', arr); Console.log (this); Console.log ('--------------'); return ' This is the ' + index + ' time ';});
2. Specify this
1 vararr1 = [' A ',, ' C ', ' d '];2 varARR2 = [' E ', ' f ', ' g ', ' H '];3Arr1.map (function(parameter, index, arr) {4Console.log ('--------------');5Console.log (' parameter: ', parameter);6Console.log (' Index: ', index);7Console.log (' arr: ', arr);8Console.log (' This ', This);9Console.log ('--------------');Ten return' This is the first ' + Index + '; One}, ARR2);
This points to our designated ARR2
3, use the arrow function, specify the This
1 vararr1 = [' A ',, ' C ', ' d '];2 varARR2 = [' E ', ' f ', ' g ', ' H '];3Arr1.map (parameter, index, arr) = {4Console.log ('--------------');5Console.log (' parameter: ', parameter);6Console.log (' Index: ', index);7Console.log (' arr: ', arr);8Console.log (' This ', This);9Console.log ('--------------');Ten return' This is the first ' + Index + '; One}, ARR2);
At this point we find that this points to the global scope of this
The difference with Array.foreach is that ForEach does not return in the process of each loop
var arr1 = [' A ',, ' C ', ' d ']; var arr2 = [' E ', ' f ', ' g ', ' H '];arr1.foreach (function(parameter, index, arr) { Console.log ('--------------'); Console.log (' parameter: ', parameter); Console.log (' index: ', index); Console.log (' arr: ', arr); Console.log (this); Console.log ('--------------'); return ' This is the ' + index + ' time ';}, arr2);
either the Map method or the ForEach method is not compatible under IE6-8!
Compatibility method:
1 /**2 * foreach traversal array3 * @param callback [function] callback function;4 * @param context [object] context;5 */6Array.prototype.myForEach =functionMyforeach (callback,context) {7Context = Context | |window;8 if(' ForEach 'inchArray.prototye) {9 This. ForEach (callback,context);Ten return; One } A //Ie6-8 Write your own callback function execution Logic - for(vari = 0,len = This. length; I < len;i++) { -Callback && Callback.call (context, This[I],i, This); the } - } - /** - * Map Traversal array + * @param callback [function] callback function; - * @param context [object] context; + */ AArray.prototype.myMap =functionMyMap (callback,context) { atContext = Context | |window; - if(' Map 'inchArray.prototye) { - return This. Map (callback,context); - } - //Ie6-8 Write your own callback function execution Logic - varNewary = []; in for(vari = 0,len = This. length; I < len;i++) { - if(typeofcallback = = = ' function ') { to varval = Callback.call (Context, This[I],i, This); +Newary[newary.length] =Val; - } the } * returnnewary; $}
Array Method-->map ()