Array.prototype.reduce = function(func,prev){
for(let i = 0 ; i < this.length;i++){
if(prev === undefined){
prev = fn(this[i],this[i+1],i,this)
}else{
prev = fn(prev,this[i],i,this)
}
return prev
}
}
Array.prototype.forEach = function(func){
for(let i = 0 ; i<this.length;i++){
func(this[i],i,this)
}
}
Array.prototype.map = function(func){
let arr = []
for(let i = 0 ; i < this.length;i++){
arr.push(func(this[i],i,this))
}
return arr
}
Array.prototype.some = function(func){
for(let i = 0 ; i < this.length;i++){
if(func(this[i])){
return true
}
}
return false
}
Array.prototype.filter = function(func){
var arr = []
for(let i = 0 ; i <this.length;i++){
if(func(this[i])){
arr.push(this[i])
}
}
return arr
}
Array.prototype.every = function(func){
for(let i = 0 ; i <this.length;i++){
if(!func(this[i])){
return false
}
}
return true
}
Array.prototype.find = function(func){
for(let i = 0 ; i <this.length;i++){
if(func(this[i])) {
return this[i]
}
}
}
数组reduce、map、forEach、some、filter、every、find实现
原创
©著作权归作者所有:来自51CTO博客作者StavinLi的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:js 行内事件JSON对象传参
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
理解 ES6 — forEach/map/reduce/filter/every/some
)...
es6 数组 字符串 跳出循环 -
数组的五中遍历方式(forEach,filter,map,some,every)
数组的五中遍历方式(forEach,filter,map,some,every)
forEach map some every filter -
filter,every,some
filter,every,some
filter every some -
linux系统上如何终止GPUID为0上的程序
大家比较熟悉 Windows 下的可执行文件,就是那些扩展名为exe的文件。大家知道,只需要鼠标双击这些程序, 就可以运行了。程序运行起来后,我们把这个程序正在运行的 实例 称之为 进程 。操作系统对每个进程都分配一个数字标记,称为 进程ID (PID)。Windows进程的信息可以通过 任务管理器看到。如下所示比如上图中,正在运行的计算器程序 Calcul
delphi pid判断进程结束 linux source关闭终端后不生效 linux 定时执行命令 linux 查看pid linux查看pid 对应的程序