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( ...
随机推荐
- Java中的基本数据据类型
1.整数类型 类型 字节数 表示范围 byte 1 -128~127 short 2 -32768 ~ 32767 int 4 -2147483648~2147483647 long 8 -92233 ...
- level3
伸冤下:老师的评论是有看到!看完我就去修改程序了,忘记回复请原谅!= = 前阵子都在修改功能和思路,但是由于一个细节的错误找不到,导致没有成品可以上传...求谅解. 细心真的很重要 = =!!! im ...
- 12th 对礼物挑选小工具的WBS功能分解
WBS功能分解: 功能 一级子功能 二级子功能 预计用时(分钟) 主页 进入相应页面 1.如果用户已处于登录状态,则返回用户登录时的主页 10 2.如果用户处于未登录状态,则返回用户预览主页. 10 ...
- 【菜鸟】RESTful 架构详解
RESTful 架构详解 分类 编程技术 1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次 ...
- Android Handler 异步调用修改界面与主线程
在Android编程的过程中,如果在Activity中某个操作会运行比较长的时间,比如:下载文件.这个时候如果在主线程中直接下载文件,会造成Activity卡死的现象:而且如果时间超过5秒,会有ANR ...
- C、C ++的内存模型
http://blog.sina.com.cn/s/blog_af9acfc60101bbcy.html
- robotframework运行时后台报错UnicodeDecodeError
win10环境下报错: Traceback (most recent call last): File "C:\Python27\lib\site-packages\robotide\con ...
- Cat VS Dog HDU - 3829 (最大独立集 )
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total ...
- Xml文档添加节点和属性
XmlDocument doc = new XmlDocument(); XmlElement xmlElement = doc.CreateElement("节点名称"); xm ...
- MT【195】三次函数
(2016年清华大学自主招生暨领军计划试题) 已知$x,y,z\in \mathbf{R}$,满足$x+y+z=1,x^2+y^2+z^2=1$,则下列结论正确的有( ) A.$xyz$的最大值为$0 ...