map方法原型:array1.map(callbackfn[, thisArg])
参数:
array1, 一个数组对象。该函数一般用于数组对象
callbackfn 函数一次。
this 值。
返回值:
一个新数组,其中的每个元素均为关联的原始数组元素的回调函数返回值。
说明:
length 属性且具有已按数字编制索引的属性名的任何对象使用。
回调函数的语法如下所示:
function callbackfn(value, index, array1)
你可使用最多三个参数来声明回调函数。
value,数组元素的值。
index,数组元素的数字索引。
array1,包含该元素的数组对象。
实例:
// Define the callback function. function AreaOfCircle(radius) { var area = Math.PI * (radius * radius); return area.toFixed(0); } // Create an array. var radii = [10, 20, 30]; // Get the areas from the radii. var areas = radii.map(AreaOfCircle); document.write(areas); // Output: // 314,1257,2827
this 关键字可引用的对象。
// Define an object that contains a divisor property and // a remainder function. var obj = { divisor: 10, remainder: function (value) { return value % this.divisor; } } // Create an array. var numbers = [6, 12, 25, 30]; // Get the remainders. // The obj argument specifies the this value in the callback function. var result = numbers.map(obj.remainder, obj); document.write(result); // Output: // 6,2,5,0
注意:在IE8以及IE8以下浏览器不支持该函数。
原文:https://msdn.microsoft.com/zh-cn/express/ff679976%28v=vs.90%29