//使用 reduce 实现数组 map 方法
const selfMap2 = function (fn, context){
let arr = Array.prototype.slice.call(this)
// 这种实现方法和循环的实现方法有异曲同工之妙,利用reduce contact起数组中每一项
// 不过这种有个弊端,会跳过稀疏数组中为空的项
return arr.reduce((pre, cur, index) => {
return [...pre, fn.call(context, cur, index, this)]
}, [])
} Array.prototype.selfMap = selfMap2
var arr1 = [1, 2, 3]
arr1.length = 5 let arrMap = arr1.selfMap(function (x) {
return x * 2
})
// [2, 4, 6]

使用 reduce 实现数组 map 方法的更多相关文章

  1. JavaScript中数组map()方法

    JavaScript 数组map()方法创建一个新的数组使用调用此数组中的每个元素上所提供的函数的结果.语法 ? 1 array.map(callback[, thisObject]); 下面是参数的 ...

  2. 循环实现数组 map 方法

    //循环实现数组 map 方法 const selfMap = function (fn, context) { let arr = Array.prototype.slice.call(this) ...

  3. 数组map方法与如何使用ES5实现

    数组map方法与如何使用ES5实现 JavaScript Array map() 方法 定义 map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值. map() 方法按照原始数 ...

  4. 归约函数reduce&映射数组map(笔记)

    function forEach(array,action){ ;i<array.length;i++) action(array[i]); } function reduce(combine, ...

  5. Array(数组)--map方法

    关于Array.prototype.map() MDN 给的定义是: 在作用数组元素的每一项上调用一个方法(callback),返回一个新数组: 使用格式:arr.map(callback[,this ...

  6. js 数组 map方法

    https://www.cnblogs.com/xuan52rock/p/4460949.html https://jingyan.baidu.com/article/91f5db1b7453471c ...

  7. ES6 数组遍历方法的实战用法总结(forEach,every,some,map,filter,reduce,reduceRight,indexOf,lastIndexOf)

    目录 forEach every some map filter reduce && reduceRight indexOf lastIndexOf 前言 ES6原生语法中提供了非常多 ...

  8. 聊一聊数组的map、reduce、foreach等方法

    聊聊数组遍历方法 JS 数组的遍历方法有好几个: every some filter foreach map reduce 接下来我们来一个个地交流下. every() arr.every(callb ...

  9. 数组reduce和map方法

    1.有一个长度为100的数组,请以优雅的方式求出该数组的前10个元素之和 var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],sum ...

随机推荐

  1. 八.OC基础加强--1.autorelease的用法 2.ARC下内存管理 3.分类(category)4.block的学习

    1.autorelease的用法   1.自动释放池及autorelease介绍 (1)在iOS程序运行过程中,会创建无数个池子,这些池子都是以栈结构(先进后出)存在的. (2)当一个对象调用auto ...

  2. bzoj3251

    3251: 树上三角形 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 637  Solved: 262[Submit][Status][Discuss ...

  3. python urllib从远程服务器下载文件到本地

    #!/usr/bin/env python #-*-coding:utf--*-' #Filename:download_file.py import sys,os import urllib def ...

  4. ContextLoaderListener与RequestContextListener配置问题

    转自:https://blog.csdn.net/yyqhwr/article/details/83381447 SSH2.SSM等web应用开发框架的配置过程中,因为都要用到spring,所以,往往 ...

  5. Winpcap笔记3之打开适配器并捕获数据包

    上一讲中知道了如何获取适配的信息,这一将我们讲写一个程序蒋每一个通过适配器的数据包打印出来. 打开设备的函数是pcap_open().函数原型是 pcap_t* pcap_open(const cha ...

  6. 4800: [Ceoi2015]Ice Hockey World Championship(折半搜索)

    4800: [Ceoi2015]Ice Hockey World Championship Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 622  S ...

  7. thinkphp数据库查重方法

    $test_data = M('hot'); //实例化数据表 $data = $test_data->Distinct(true)->field('descriprion')->o ...

  8. 【已解决】python中文字符乱码(GB2312,GBK,GB18030相关的问题)

      http://againinput4.blog.163.com/blog/static/1727994912011111011432810/ [已解决]python中文字符乱码(GB2312,GB ...

  9. 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  10. MyEclipse2014+Maven配置记录

    一.MyEclipse配置Maven 打开MyEclipse2014,选择菜单:Window --> Preferences,选择:MyEclipse-Maven4MyEclipse-Insta ...