// ES5循环循环实现filter

const selfFilter = function (fn, context) {

    let arr = Array.prototype.slice.call(this)

    let filteredArr = []

    for (let i = 0; i < arr.length; i++) {

        if(!arr.hasOwnProperty(i)) continue;

         fn.call(context, arr[i], i, this) && filteredArr.push(arr[i])

    }

    return filteredArr

}

// ES5循环实现map

const selfMap = function (fn, context) {

    let arr = Array.prototype.slice.call(this)

    let mappedArr = []

    for (let i = 0; i < arr.length; i++) {

        // 判断稀疏数组的情况

        if (!arr.hasOwnProperty(i)) continue;

        mappedArr.push(fn.call(context, arr[i], i, this))

    }

    return mappedArr

}

// reduce实现map

const selfMap2 = function (fn, context) {

    let arr = Array.prototype.slice.call(this)

    return arr.reduce((pre, cur, index) => {

        return [...pre, fn.call(context, cur, index, this)]

    }, [])

}

es5实现map/filter的更多相关文章

  1. 【原】javascript笔记之Array方法forEach&map&filter&some&every&reduce&reduceRight

    做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES ...

  2. python 内置函数 map filter reduce lambda

    map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...

  3. 如何在python3.3用 map filter reduce

    在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0  ...

  4. Swift map filter reduce 使用指南

    转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...

  5. map filter 的func 放在前面

    map    filter      的func 放在前面 sorted 在后 (    iter..  ,       key=function')

  6. Python之内建函数Map,Filter和Reduce

    Python进阶 map,filter, reduce是python常用的built-in function. 且常与lambda表达式一起用. 其中: map 形式:map(function_to_ ...

  7. Map,Filter和Reduce

    转自:https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG 摘要:Map,Filter和Reduce三个函数 ...

  8. Python Map, Filter and Reduce

    所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...

  9. [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE

    map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...

随机推荐

  1. springboot 集成swagger2.x 后静态资源报404

    package com.bgs360.configuration; import org.springframework.context.EnvironmentAware; import org.sp ...

  2. python下vs的使用

    part 1:导入pygame包 在python环境下:视图---其他窗口--python环境,选择从pypi安装pygame

  3. SQL Update多表联合更新的方法

    SQL Update多表联合更新的方法 (1) sqlite 多表更新方法 update t1 set col1=t2.col1 from table1 t1 inner join table2 t2 ...

  4. ubuntu nvidia驱动安装教程

    1. 安装显卡切换软件 sudo add-apt-repository ppa:nilarimogard/webupd8 #添加PPA更新源   sudo apt-get update #刷新更新源列 ...

  5. token的解码及 判断值不为空的方法

    token 的解码要使用插件:jwt-decode 判断值不为空的方法: function isEmpty(value){ return ( value === undefined || value ...

  6. ffmpe转码添加时间水印

    ffmpeg出直播流的时候 添加时间水印的话可以观察延迟 录制也方便查问题下面的命令 需要用到的字体自己百度下载一下或者到C:\Windows\Fonts复制一下 ffmpeg -stream_loo ...

  7. CentOS 7服务器下Nginx安装配置

    一.安装编译工具及库文件 $ yum -y install make zlib zlib-devel gcc gcc-c++ libtool openssl openssl-devel pcre pc ...

  8. C# List中的ForEach

    ; List<string> aaa = new List<string>(){ "aaa", "bbb" }; aaa.ForEach ...

  9. @Transactional(事务讲解)和springboot 整合事务

    概述 事务在编程中分为两种:声明式事务处理和编程式事务处理 编程式事务处理:编码方式实现事务管理,常与模版类TransactionTemplate(推荐使用) 在业务代码中实现事务. 可知编程式事务每 ...

  10. ios 报错记录

    1. 运行xcode 报错:unterminated conditional directive #ifdef 缺少对应的#endif 在结尾加上就好了 2.iOS添加非(c,c++)文件引发的&qu ...