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( ...
随机推荐
- 第十五次ScrumMeeting博客
第十五次ScrumMeeting博客 本次会议于12月4日(一)22时整在3公寓725房间召开,持续30分钟. 与会人员:刘畅.辛德泰.张安澜.赵奕.方科栋. 1. 每个人的工作(有Issue的内容和 ...
- PAT甲题题解-1063. Set Similarity (25)-set的使用
题意:两个整数集合,它们的相似度定义为:nc/nt*100%nc为两个集合都有的整数nt为两个集合一共有的整数注意这里的整数都是各不相同的,即重复的不考虑在内.给出n个整数集合,和k个询问,让你输出每 ...
- PAT甲题题解-1096. Consecutive Factors(20)-(枚举)
题意:一个正整数n可以分解成一系列因子的乘积,其中会存在连续的因子相乘,如630=3*5*6*7,5*6*7即为连续的因子.给定n,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可 ...
- Mac 绑定Gitlab或者GitHub帐号,从新生成公钥
1.SSH(Secure Shell)是一种安全协议,在你的电脑与GitLab服务器进行通信时,我们使用SSH密钥(SSH Keys)认证的方式来保证通信安全. 2.创建 SSH密钥,并将密钥中的公钥 ...
- ElasticSearch 5.6.1 安装 Kibana、X-Pack和head
前面已经有写过ElasticSearch和iK的安装了这里就不在所了. 安装Kiabna 在下载tar包的时候需要注意下一安装的es版本号,按照官网的说明版本是对应一致的. https://www.e ...
- 【Leetcode】209. Minimum Size Subarray Sum
Question: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- 使用Webdriver刷博客文章评论
package com.zhc.webdriver; import java.util.ArrayList; import java.util.Iterator; import java.util.c ...
- Good Bye 2018 没打记
场外选手赛时只口胡出了CD感觉非常惨.只看了E并且还没看到题面里的wiki我能咋办 C:f只与gcd(n,k)有关. D:考虑每种起始位置,对于跨越的两个排列,只有前一个排列的后缀单减时不产生贡献.答 ...
- BZOJ2721 Violet5樱花(数论)
有(x+y)n!=xy.套路地提出x和y的gcd,设为d,令ad=x,bd=y.则有(a+b)n!=abd.此时d已是和a.b无关的量.由a与b互质,得a+b与ab互质,于是将a+b除过来得n!=ab ...
- 安装及调试 Mavem Web
一 使用Mavem eclipse菜单栏,找到file-->new -->other 然后找到Maven Project 然后next. 接着,选择maven-archetype-web ...