js数组方法forEach,map,filter,every,some实现
Array.prototype.map = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError(); var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
} return res;
}; Array.prototype.filter = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError(); var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
} return res;
}; Array.prototype.some = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError(); var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this && fun.call(thisp, this[i], i, this))
return true;
} return false;
}; Array.prototype.every = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError(); var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this && !fun.call(thisp, this[i], i, this))
return false;
} return true;
}; Array.prototype.forEach = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError(); var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
Array.prototype.map = function(fun /*, thisp*/)
{var len = this.length;
if (typeof fun != "function")
thrownewTypeError();
var res = newArray(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i inthis)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
Array.prototype.filter = function(fun /*, thisp*/)
{var len = this.length;
if (typeof fun != "function")
thrownewTypeError();
var res = newArray();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i inthis)
{
var val = this[i]; // in case fun mutates thisif (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
Array.prototype.some = function(fun /*, thisp*/)
{var len = this.length;
if (typeof fun != "function")
thrownewTypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i inthis && fun.call(thisp, this[i], i, this))
returntrue;
}
returnfalse;
};
Array.prototype.every = function(fun /*, thisp*/)
{var len = this.length;
if (typeof fun != "function")
thrownewTypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i inthis && !fun.call(thisp, this[i], i, this))
returnfalse;
}
returntrue;
};
Array.prototype.forEach = function(fun /*, thisp*/)
{var len = this.length;
if (typeof fun != "function")
thrownewTypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i inthis)
fun.call(thisp, this[i], i, this);
}
};
js数组方法forEach,map,filter,every,some实现的更多相关文章
- ES6新增的常用数组方法(forEach,map,filter,every,some)
ES6新增的常用数组方法 let arr = [1, 2, 3, 2, 1]; 一 forEach => 遍历数组 arr.forEach((v, i) => { console.log( ...
- ES6 数组方法 forEach map filter find every some reduce
1. forEach const colors = ['red', 'blue', 'green'] colors.forEach(function (params) { console.log(pa ...
- 【原】javascript笔记之Array方法forEach&map&filter&some&every&reduce&reduceRight
做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES ...
- 处理数组的forEach map filter的兼容性
处理数组的forEach //forEach处理 if(!Array.prototype.forEach) { Array.prototype.forEach = function (callback ...
- js 数组方法比较
js 数组方法比较 table th:first-of-type { width: 80px; } table th:nth-of-type(2) { width: 120px; } table th ...
- js数组方法详解
Array对象的方法-25个 /*js数组方法详解 */ /* * 1 concat() 用于连接多个数组或者值-------------- * 2 copyWithin() 方法用于从数组的指定位置 ...
- 转载收藏(js数组方法大全)
js数组方法大全 JavaScript中创建数组有两种方式 (一)使用 Array 构造函数: var arr1 = new Array(); //创建一个空数组var arr2 = new Arra ...
- js数组方法大全(下)
# js数组方法大全(下) 记录一下整理的js数组方法,免得每次要找方法都找不到.图片有点多,注意流量,嘻嘻! 本期分享 forEach() map() filer() every() some() ...
- js数组方法大全(上)
# js数组方法大全(上) 记录一下整理的js数组方法,免得每次要找方法都找不到.图片有点多,注意流量,嘻嘻! 本期分享 join() reverse() sort() concat() slice( ...
随机推荐
- Ajax实例OR技术原理 转自 (http://blog.csdn.net/evankaka )
摘要:AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术.AJAX 是一种用于创建快速动态网页的 ...
- 2-Eleventh Scrum Meeting20151211
第二阶段任务分工整理会议 1.会议任务: (1)明晰第二阶段的开发内容,统计未完成的功能留需完善开发. (2)安排任务分工,每个人的工作安排. (3)PM职位担任. (4)博客内容负责. 2.会议记录 ...
- Linux内核第一节
存储程序计算机工作模型 存储程序计算机——冯诺依曼体系结构 IP:寄存器,总是指向内存的代码段.IP(16位) 32位(EIP) 64位(RIP). 内存:保存数据和指令. CPU:CPU从IP指向的 ...
- 炸弹人的Alpha版使用说明
本游戏是一款手机游戏,学生可以在无聊时打发时间,放松心情.现在只有三关,但游戏运行还算可以. 注意事项: 目前游戏还有一些不好的地方,游戏无法暂停,如果游戏任务死亡,则无法重开. 游戏后面的关卡还需要 ...
- Spark 实践——基于 Spark MLlib 和 YFCC 100M 数据集的景点推荐系统
1.前言 上接 YFCC 100M数据集分析笔记 和 使用百度地图api可视化聚类结果, 在对 YFCC 100M 聚类出的景点信息的基础上,使用 Spark MLlib 提供的 ALS 算法构建推荐 ...
- 2013长春网赛1004 hdu 4762 Cut the Cake
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4762 题意:有个蛋糕,切成m块,将n个草莓放在上面,问所有的草莓放在同一块蛋糕上面的概率是多少.2 & ...
- 原理分析dubbo分布式应用中使用zipkin做链路追踪
zipkin是什么 Zipkin是一款开源的分布式实时数据追踪系统(Distributed Tracking System),基于 Google Dapper的论文设计而来,由 Twitter 公司开 ...
- MT【179】最大最小老问题
求$\max\{x^2+2y+20,y^2-6x+12\}$的最小值______ 提示:$4t\ge 3(x^2+2y+20)+y^2-6x+12=3(x-1)^2+(y+3)^2+60\ge 60, ...
- 动态生成web表-asp.net table
1. 页面上定义一个server 的table <table style="width: 100%" id="tbContent" runat=" ...
- 【bzoj3172】 Tjoi2013—单词
http://www.lydsy.com/JudgeOnline/problem.php?id=3172 (题目链接) 题意 $n$个单词组成文本,问每个单词在文本中出现了几次. Solution 题 ...