
手写数组方法
发布日期:2021-05-07 23:09:01
浏览次数:17
分类:精选文章
本文共 1320 字,大约阅读时间需要 4 分钟。
封装一个forEach
Array.prototype.myForEach = function (fun) { for (var i in this) { if (this.hasOwnProperty(i)) { fun(this[i], i, this); } }}
封装一个filter
Array.prototype.myFilter = function (fun) { var arr = []; //定义一个新的数组 for (var i in this) { //如果为真,留下 if (this.hasOwnProperty(i)) fun(this[i], i, this) ? arr.push(this[i]) : null; } return arr;//重新返回一个数组}
封装一个Map 映射 x->y
Array.prototype.myMap = function (fun) { var arr = []; for (var i in this) { if (this.hasOwnProperty(i)) arr.push(fun(this[i], i, this)); //返回一个新数组 } return arr;}
封装Every
Array.prototype.myEvery = function (fun) { var a = null; for (var i in this) { if (this.hasOwnProperty(i)) a = fun(this[i], i, this); if (!a) break; } return a;}
封装some
Array.prototype.mySome = function (fun) { var a = null; for (var i in this) { if (this.hasOwnProperty(i)) a = fun(this[i], i, this); if (a) break; } return a;}
封装reduce
Array.prototype.myReduce = function (fun, obj) { var prep = obj; for (var i in this) { if (this.hasOwnProperty(i)) { prep = fun(prep, this[i], i, this) } } return prep;}
发表评论
最新留言
很好
[***.229.124.182]2025年04月06日 04时59分27秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
使用async、await改善异步代码
2019-03-04
Idiot 的间谍网络
2019-03-04
初探SSRF漏洞
2019-03-04
pythonBug入门——从零开始学python
2019-03-04
js-禁止右键菜单代码、禁止复制粘贴代码
2019-03-04
SpringBoot中使用Mybatis访问MySQL数据库(使用xml方式)
2019-03-04
数组--Go语言学习笔记
2019-03-04
Redis (三)——Linux 上安装 Redis
2019-03-04
java 重写(override)和重载(overload)区别
2019-03-04
java 多态类型转换
2019-03-04
常用正则表达式
2019-03-04
XML:采用XHTML和CSS设计可重用可换肤的WEB站点
2019-03-04
Java判断字符串是否为金额
2019-03-04
软件架构-zookeeper快速入门
2019-03-04
angr学习笔记(7)(malloc地址单元符号化)
2019-03-04
树状数组 模板总结
2019-03-04
结构型设计在工作中的一些经验总结
2019-03-04
如何提升员工体验 助力企业业务增长?这个棘手的问题终于被解决了!
2019-03-04
2020 AI 产业图谱启动,勾勒中国 AI 技术与行业生态
2019-03-04
Netty4服务端入门代码示例
2019-03-04