es5实现map/filter
// 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的更多相关文章
- 【原】javascript笔记之Array方法forEach&map&filter&some&every&reduce&reduceRight
做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES ...
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- 如何在python3.3用 map filter reduce
在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0 ...
- Swift map filter reduce 使用指南
转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...
- map filter 的func 放在前面
map filter 的func 放在前面 sorted 在后 ( iter.. , key=function')
- Python之内建函数Map,Filter和Reduce
Python进阶 map,filter, reduce是python常用的built-in function. 且常与lambda表达式一起用. 其中: map 形式:map(function_to_ ...
- Map,Filter和Reduce
转自:https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG 摘要:Map,Filter和Reduce三个函数 ...
- Python Map, Filter and Reduce
所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...
- [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE
map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...
随机推荐
- vue04 总结
""" 1.环境 node:官网下载安装包,傻瓜式安装 - https://nodejs.org/zh-cn/ => 附带按照了npm cnpm:npm insta ...
- oracle 分配表权限给用户的写法
grant select on xxxx.xxxxx_TB to sb;grant select on xxxx.xxxxxx_tb to sb;
- Jmeter测试部全体学习
Jmeter小助手:__counter __Random __UUID __CSVRead 性能指标:CPU 内存 磁盘 网络 版本(系统版本) linux命令: top 能够试 ...
- 【Python之路】特别篇--基于领域驱动模型架构设计的京东用户管理后台
一.预备知识: 1.接口: - URL形式 - 数据类型 (Python中不存在) a.类中的方法可以写任意个,想要对类中的方法进行约束就可以使用接口: b.定义一个接口,接口中定义一个方法f1: c ...
- import org.apache.ibatis.annotations.Param 报错
说明缺少依赖 <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus ...
- (十八)链接数据库,QSqlTableModel
QMYSQL——mysql QSQLITE——sqlite QOICQ——orcale 所需头文件 .pro增加 sql #include <QSqlDatabase> #include ...
- axios 是如何封装 HTTP 请求的
原载于 TutorialDocs 网站的文章<How to Implement an HTTP Request Library with Axios>.译者:zhangbao90shttp ...
- Liunx反弹shell的几种方式
什么是反弹shell? 简单理解,通常是我们主动发起请求,去访问服务器(某个IP的某个端口),比如我们常访问的web服务器:http(https)://ip:80,这是因为在服务器上面开启了80端口的 ...
- vue实战教程
转载自 https://www.cnblogs.com/sunsets/p/7760454.html
- windows下安装MongoDB服务
1,参考:https://www.cnblogs.com/lecaf/p/mongodb.html 2,要设置环境变量 3,设置用户 use admin 注:MongoDB安装好以后由默认的admi ...