JavaScript数组方法map
map方法是我最喜欢的方法了,虽然简单,但功能强大,用起来非常方便。在ECMPAScript 5中也添加了这个方法。
map()方法功能:将调用的数组的每个元素传递给指定的函数,并返回一个数组,该数组由函数的返回值构成。
注:map返回的是新数组,它不修改调用的数组。
为了兼容不支持map的浏览器,developer.mozilla.org上给出了map兼容性解决方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
// Production steps of ECMA-262, Edition 5, 15.4.4.19
// Reference: http://es5.github.com/#x15.4.4.19
if (!Array.prototype.map) {
Array.prototype.map = function (callback, thisArg) {
var T, A, k;
if ( this == null ) {
throw new TypeError( " this is null or not defined" );
}
// 1. Let O be the result of calling ToObject passing the |this| value as the argument.
var O = Object( this );
// 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;
// 4. If IsCallable(callback) is false, throw a TypeError exception.
// See: http://es5.github.com/#x9.11
if ( typeof callback !== "function" ) {
throw new TypeError(callback + " is not a function" );
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (thisArg) {
T = thisArg;
}
// 6. Let A be a new array created as if by the expression new Array(len) where Array is
// the standard built-in constructor with that name and len is the value of len.
A = new Array(len);
// 7. Let k be 0
k = 0;
// 8. Repeat, while k < len
while (k < len) {
var kValue, mappedValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal method of O with argument Pk.
kValue = O[ k ];
// ii. Let mappedValue be the result of calling the Call internal method of callback
// with T as the this value and argument list containing kValue, k, and O.
mappedValue = callback.call(T, kValue, k, O);
// iii. Call the DefineOwnProperty internal method of A with arguments
// Pk, Property Descriptor {Value: mappedValue, : true, Enumerable: true, Configurable: true},
// and false.
// In browsers that support Object.defineProperty, use the following:
// Object.defineProperty(A, Pk, { value: mappedValue, writable: true, enumerable: true, configurable: true });
// For best browser support, use the following:
A[ k ] = mappedValue;
}
// d. Increase k by 1.
k++;
}
// 9. return A
return A;
};
}
|
转载于:https://blog.51cto.com/shaou/1307430
JavaScript数组方法map相关推荐
- js 数组从头添加到数组_如何从头开始实现JavaScript数组方法
js 数组从头添加到数组 介绍 (Introduction) JavaScript includes several functions for working with arrays that go ...
- JavaScript 数组方法 遍历
JavaScript 数组方法 创建一个数组 Concat()连接两个或更多的数组 并返回结果 join()用指定分隔符分隔数组并转换为字符串 Push()可向数组的末尾添加一个或多个元素,并返回 ...
- JavaScript数组方法学习(一):数组元素的增加和删除
JavaScript数组方法学习(一):数组元素的增加和删除 文章目录 JavaScript数组方法学习(一):数组元素的增加和删除 前言 一.往数组里新增元素 1.在数组的最前面添加:unshift ...
- JavaScript数组方法大全解
0 前言 本文只讲解标准化方法,而不会讲解实验性方法,如at().groupBy().groupByMap()和toSource(). 数组中的部分方法需要提供区间范围begin/start和end, ...
- JavaScript数组方法大全(推荐)
数组在笔试中经常会出现的面试题,javascript中的数组与其他语言中的数组有些不同,为了方便之后数组的方法学习,下面小编给大家整理了关于数组的操作方法,一起看看吧. 数组创建 JavaScript ...
- JavaScript数组方法速查手册
32个数组的常用方法和属性 一.数组属性 length - 长度属性 var arr = [ 'a', 'b', 'c' ]; console.log(arr.length); // 输出 3 // ...
- JavaScript数组方法终极指南-地图
The map() method applies a function to each element in an array and returns a copy of the original a ...
- JavaScript数组方法(最新)包含ES10方法
JS数组方法(最新) 数组方法 一.数组方法(添加,删除) 1.push() 2.pop() 3.unshift() 4.shift() 二.数组方法(插入,连接,反转,排序,剪切,拼接) 1.spl ...
- JavaScript数组方法大全(分为会不会改变原数组)
若看不懂注释的输出方式可以将代码粘贴到浏览器中执行观看 1. 数组通用方法 Array.from: 将一个类数组转化成数组 类数组:Set, Map,对象等可遍历对象皆为类数组 Array.from( ...
最新文章
- 【CV】ECCV2020图像分割开源论文合集
- Java基础知识——Java集合详解
- 人形机器人正在美国史密森尼博物馆中担任导游的工作
- 关于DPM(Deformable Part Model)算法中模型结构的解释
- JAVA SE知识点总结
- mysql备份为0_MySQL 备份(一)
- 北京数学建模与计算机应用2018,2018年第八届MathorCup高校数学建模挑战赛
- STM8S103之时钟设置
- 全年日降雨数据下载与处理教程
- 里程碑!美国航天局NASA耗资高达百亿美元,“终极太空望远镜”拍到了什么?| 美通社头条...
- 一个前端资源站 http://www.bootcss.com/
- SSM搭建-Spring之bean的属性值XML注入方式(4)
- 卷积神经网络学习路线(十九) | 旷世科技 2017 ShuffleNetV1
- 随便学学Python-day7-字典和集合
- 电信客户流失数据分析(一)
- 趣图:看到网友晒了新抱枕,我也想换个新的了
- 创建telegram 机器人
- 关闭阿里云的短信提醒
- Oracle-数据库所有查询命令
- [渝粤教育] 西南科技大学 建筑CAD 在线考试复习资料(1)
热门文章
- 7-41 大数的乘法 (10 分)
- 4025-数组的正负排序(C++,双指针法,附思路)
- 2021二维数组中的元素查重(C++,stl--set)
- linux 模拟运行 微信,Ubuntu 18.04 安装微信(Linux通用)
- selenium通过加载火狐Firefox配置文件FirefoxProfile,实现免登陆访问网站
- QT-helloworld-Qt设计师编写
- 人人开源之代码生成器(renren-generator)
- [BZOJ 4571][Scoi2016]美味(主席树)
- 浅谈MySql的存储引擎(表类型)
- 诗和远方:无题(五十二)- 写给认识的一个老姐的两只猫