1. 原理
使用回调函数处理数组中的每一项
2. 源码实现
Array.prototype.Imap=function (callback){
let newArray = []
this.forEach((old)=>{
let newData = callback(old)
newArray.push(newData )
})
return newArray
}
3. 栗子体现
const arr = [1,2,3,4,5,6]
const newArr = arr.Imap((v)=>{
return v*2
})
console.log(newArr) //[2, 4, 6, 8, 10, 12]