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实现的更多相关文章

  1. ES6新增的常用数组方法(forEach,map,filter,every,some)

    ES6新增的常用数组方法 let arr = [1, 2, 3, 2, 1]; 一 forEach => 遍历数组 arr.forEach((v, i) => { console.log( ...

  2. ES6 数组方法 forEach map filter find every some reduce

    1. forEach const colors = ['red', 'blue', 'green'] colors.forEach(function (params) { console.log(pa ...

  3. 【原】javascript笔记之Array方法forEach&map&filter&some&every&reduce&reduceRight

    做前端有多年了,看过不少技术文章,学了新的技术,但更新迭代快的大前端,庞大的知识库,很多学过就忘记了,特别在项目紧急的条件下,哪怕心中隐隐约约有学过一个方法,但会下意识的使用旧的方法去解决,多年前ES ...

  4. 处理数组的forEach map filter的兼容性

    处理数组的forEach //forEach处理 if(!Array.prototype.forEach) { Array.prototype.forEach = function (callback ...

  5. js 数组方法比较

    js 数组方法比较 table th:first-of-type { width: 80px; } table th:nth-of-type(2) { width: 120px; } table th ...

  6. js数组方法详解

    Array对象的方法-25个 /*js数组方法详解 */ /* * 1 concat() 用于连接多个数组或者值-------------- * 2 copyWithin() 方法用于从数组的指定位置 ...

  7. 转载收藏(js数组方法大全)

    js数组方法大全 JavaScript中创建数组有两种方式 (一)使用 Array 构造函数: var arr1 = new Array(); //创建一个空数组var arr2 = new Arra ...

  8. js数组方法大全(下)

    # js数组方法大全(下) 记录一下整理的js数组方法,免得每次要找方法都找不到.图片有点多,注意流量,嘻嘻! 本期分享 forEach() map() filer() every() some() ...

  9. js数组方法大全(上)

    # js数组方法大全(上) 记录一下整理的js数组方法,免得每次要找方法都找不到.图片有点多,注意流量,嘻嘻! 本期分享 join() reverse() sort() concat() slice( ...

随机推荐

  1. Final阶段用户使用报告

    此作业要求参见:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2477] 组名:可以低头,但没必要 组长:付佳 组员:张俊余 李文涛 孙 ...

  2. Linux内核读书笔记第二周

    什么是系统调用 简单来说,系统调用就是用户程序和硬件设备之间的桥梁.用户程序在需要的时候,通过系统调用来使用硬件设备. 系统调用的存在,有以下重要的意义: 1)用户程序通过系统调用来使用硬件,而不用关 ...

  3. 剑指offer:二叉树的深度

    题目描述: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 解题思路: 这道题也是递归的思路,比较简单. 做的过程中遇到的一个 ...

  4. [BUG随想录] 看不见的分隔符: Zero-width space

    今天在调试一段代码的时候,有一个输入不能为空的库函数抛出了异常(为空就会抛出异常,就是这么傲娇).自己暗骂了自己一番,怎么这么大意,于是追溯源头,开始寻找输入控制的地方.但是当我找到时我惊呆了,我明明 ...

  5. [转帖]Windows 使用netsh 命令行方式处理 windows防火墙的方法

    Windows防火墙命令行手册 https://blog.csdn.net/mystudyblog0507/article/details/79617629 简介 netsh advfirewall ...

  6. npm 报错unable to verify the first certificate

    npm总是报错:unable to verify the first certificate 原创 2017年09月30日 11:06:10 https://blog.csdn.net/nicexib ...

  7. sql 索引 sql_safe_updates

    为了数据的安全性,mysql有一个安全性设置,sql_safe_updates ,当把这个值设置成1的时候,当程序要对数据进行修改删除操作的时候条件必须要走索引. 刚好现在也碰到了此类问题:网上找了相 ...

  8. Spring注解开发简要步骤

    1.除spring基本包外还需要下载AOP包 spring-aop-4.2.4.RELEASE.jar 2.导入约束(最后两行) <beans xmlns="http://www.sp ...

  9. maven项目编译运行时提示jdk版本过低问题解决方法

    明明使用的是1.8jdk,但是运行项目时提示使用的是java版本是1.5,版本过低. 修改pom.xml,添加如下: <build> <plugins> <plugin& ...

  10. Python之路:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...