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( ...
随机推荐
- 就qq软件的优缺点
qq对于现在的人来说,可谓是无所不知的,这也使得它迅速融入到人们的生活中,但它也是一把双刃剑,就优缺点我进行一下举例说明: 它的优点:qq由最初设计的一种聊天工具现在已经发展成为一个很全面多用途的工具 ...
- 关于Backbone和Underscore再说几点
1. Backbone本身没有DOM操作功能,所以我们需要导入JQuery/Zepto/Ender 2. Backbone依赖于underscore.js: http://documentcloud. ...
- 【Alpha】第九次Scrum meeting
今日任务一览: 姓名 今日完成任务 所耗时间 刘乾 由于发现之前版本的模板引擎存在致命bug,所以重新更换,现在使用jinja2作为模板渲染引擎. 3 鲁聃 卤蛋今天为了给编译老师做视频,没有做软工项 ...
- ajax异步导致js方法顺序执行不了
js两个方法调用的顺序,有时候是这样的 f1(); f2(); 本来是先执行f1的,但是如果f1里面进行ajax异步 async:true,那么可能会先执行f2,如果想要顺序执行,那么就把异步设 ...
- OVS 精确删除流表
OVS 精确删除流表 前言 今天看了sdnlab小姐姐的mininet教程之后,看到有一个下流表的操作,优先级没能下成功,然后回来实验一下,这个问题是解决了,不过遇到了一个小问题,ovs如何精确删除流 ...
- ElasticSearch 2 (4) - API Convention
ElasticSearch 2.1.1 (4) - API Convention The elasticsearch REST APIs are exposed using JSON over HTT ...
- 11th 最后的致意
“终于我们不再是师生”,无论日后我们是否是师生,但这段经历是不可否认的,可以说软件工程这一门课程恐怕是我学生生涯中终生难忘的一段体验.即便不是从知识上,从另一个方面来讲,也教给了我一种做人做事的态度. ...
- Java之utf8中文编码转换
import org.apache.commons.codec.binary.Hex; import org.junit.Test; /** * HBASE中文转换 */ public class C ...
- shell之三大文本处理工具grep、sed及awk
grep.sed和awk都是文本处理工具,虽然都是文本处理工具单却都有各自的优缺点,一种文本处理命令是不能被另一个完全替换的,否则也不会出现三个文本处理命令了.只不过,相比较而言,sed和awk功能更 ...
- Day23-Model操作,Form操作和序列化操作
参考源出处:http://blog.csdn.net/fgf00/article/details/54629502 1. 搭建环境请参考:http://www.cnblogs.com/momo8238 ...