博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript中数组的map方法
阅读量:4610 次
发布时间:2019-06-09

本文共 1443 字,大约阅读时间需要 4 分钟。

map方法原型:array1.map(callbackfn[, thisArg])

参数:

array1,必选。 一个数组对象。该函数一般用于数组对象

callbackfn,必选。 最多可以接受三个参数的函数。 对于数组中的每个元素,map 方法都会调用 callbackfn 函数一次。

thisArg,可选。 callbackfn 函数中的 this 关键字可引用的对象 如果省略 thisArg,则 undefined 将用作 this 值。

返回值:

一个新数组,其中的每个元素均为关联的原始数组元素的回调函数返回值

说明:

对于数组中的每个元素,map 方法都会调用 callbackfn 函数一次(采用升序索引顺序) 将不会为数组中缺少的元素调用回调函数。除了数组对象之外,map 方法可由具有 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

下面的示例阐释 thisArg 参数的用法,该参数指定 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

 

转载于:https://www.cnblogs.com/digdeep/p/4413991.html

你可能感兴趣的文章
Luogu P3393 逃离僵尸岛
查看>>
Flatten Binary Tree to Linked List
查看>>
Edit Distance
查看>>
软件工程第一次作业补充
查看>>
N76E003---输入捕获
查看>>
poj 1094 Sorting It All Out(拓扑排序)
查看>>
acdream B - 郭式树 (水题 卡cin,cout, 卡LL)
查看>>
BMP图像格式
查看>>
python的匿名函数lambda解释及用法
查看>>
c#遍历Dictionary使用KeyValuePair
查看>>
defineProperties属性的运用==数据绑定
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>
三月23日测试Fiddler
查看>>
20171013_数据库新环境后期操作
查看>>
poj 1654 && poj 1675
查看>>
运维派 企业面试题1 监控MySQL主从同步是否异常
查看>>