Map method Prototype:Array1.map (callbackfn[, Thisarg])
Parameters:
Array1, must be selected. an Array object. This function is typically used for array objects
CALLBACKFN, must be selected. A function that can accept up to three parameters. for each element in the array, themap method calls the Callbackfn function one time.
Thisarg, optional. The This keyword in the CALLBACKFN function can refer to the object . if Thisarg is omitted , undefined is used as the this value.
return value:
a new array in which each element is the return value of the callback function for the associated original array element .
Description
for each element in the array, the map method calls the CALLBACKFN function once (in ascending index order). The callback function will not be called for missing elements in the array. In addition to array objects, themap method can be used by any object that has a length property and has a property name indexed by number.
The syntax for the callback function is as follows:
function Callbackfn (value, index, array1)
You can declare a callback function with up to three parameters.
Value of the array element.
Index, the number of the array element.
An array1 that contains the array object for the element.
Instance:
//Define the callback function.functionareaofcircle (RADIUS) {varArea = Math.PI * (RADIUS *radius); returnArea.tofixed (0); } //Create an array.varradii = [10, 20, 30]; //Get The areas from the radii.varAreas =Radii.map (areaofcircle); document.write (areas); //Output://314,1257,2827
The following example illustrates the use of the thisarg parameter, which specifies the object that the This keyword can refer to.
//Define An object, contains a divisor property and//a remainder function.varobj ={divisor:10, remainder:function(value) {returnValue% This. Divisor; } } //Create an array.varnumbers = [6, 12, 25, 30]; //Get the remainders.//The obj argument specifies the this value in the callback function.varresult =Numbers.map (Obj.remainder, obj); document.write (result); //Output://6,2,5,0
Note: This function is not supported in IE8 and IE8 browsers below.
Original: Https://msdn.microsoft.com/zh-cn/express/ff679976%28v=vs.90%29
Map method of arrays in JavaScript