forEach
Array.prototype.forEach = function(cb) {
for(let i = 0 ; i< this.length; i++){
cb(this[i],i,this)
}
}
map
Array.prototype.map = function(cb) {
let newArray = []
for(let i=0;i<this.length;i++){
let obj = cb(this[i],i,this)
newArray.push(obj)
}
return newArray
}
filter
Array.prototype.filter=function(cb){
let newArray=[]
for(let i=0;i<this.length;i++){
let boolean = cb(this[i],i,this)
if(boolean) newArray.push(this[i])
}
return newArray
}
reduce
Array.prototype.reduce=function(cb,initValue) {
let sum=initValue ? initValue : this[0]
let start = !initValue ? 1 : 0
for(let i=start ;i<this.length;i++){
sum = cb(sum,this[i],i)
}
return sum
}
flat
Array.prototype.flat=function(){
let arr = this
while(arr.some(key=>Array.isArray(key))){
arr = [].concat(...arr)
}
return arr
}