数组高阶方法

129 阅读1分钟

1: forEach

Array.prototype.foreach1 = function (aaa) {
for (let i = 0; i < this.length; i++) {
aaa(this[i], i)
}
} //在arr中添加一个foreach方法
//prototype
arr.foreach1(function (a, b) {
console.log(a, b)
})

2:some

 // Array.prototype.some1 = function (aaa) {
// for (let i = 0; i < this.length; i++) {
// // aaa(this[i], i)
// if (aaa(this[i], i)){
// return true
// }
// }
// return false
// }
// let a = arr.some1(function (item) {
// return item < 20
// })
// console.log(a);

3:every

  // Array.prototype.every1 = function (aaa) {
// for (let i = 0; i < this.length; i++) {
// // aaa(this[i], i)
// if (!(aaa(this[i], i))){
// return false
// }
// }
// return true
// }
// arr.every1 ()
// let zzz= function (item) {
// return item < 20
// }
// console.log(a);

4:filter

Array.propotype.filter1 = function(callback){
let res = []
for(let i = 0;i
let istrue = callback(this[i],i)
if(isture){
res.push(this[i])
} return res
}
}

5:map

Array.propotype.map = function (callback) {
let res = []
for (let i = 0; i < this.length; i++) {
let obj = callback(this[i], i)
res.push(obj)
return res
}
}