数组声明式系列方法:map、reduce、filter、some等的实现

tech2022-09-18  106

map()

Array.prototype.myMap = function (callback) { const arr = [] // 遍历当前数组每个元素, 调用callback得到一个结果数据, 添加arr for (let index = 0; index < this.length; index++) { const element = this[index]; const result = callback(element, index) arr.push(result) } return arr } // 定义数组 const arr = [55,22,11,33,44] // map(callback),接收一个函数为参数,返回一个加工后的新数组 const res = arr.myMap((item,index) => item + 10) // 打印 console.log(res)

reduce()

Array.prototype.myReduce = function (callback, initValue) { // 结果为初始值 let total = initValue // 遍历当前数组每个元素, 调用callback得到结果数据,以供下一次计算 for (let index = 0; index < this.length; index++) { const element = this[index]; // 使用total保存每次回调返回的值 total = callback(total, element, index) } // 返回结果 return total } // 定义数组 const arr = [55,22,11,33,44] // 使用 const res = arr.myReduce((pre,item) => pre *= item) // 打印 console.log(res)

filter()

Array.prototype.myFilter = function (callback) { const arr = [] // 遍历当前数组每个元素 for (let index = 0; index < this.length; index++) { const element = this[index]; // 调用callback得到一个布尔值 const result = callback(element, index) // 如果为true, 将当前element添加到arr if (result) { arr.push(element) } } // 返回这个数组 return arr } // 定义数组 const arr = [55,22,11,33,44] // 使用 const resArr = arr.myFilter((item,index)=>{ console.log(item,index) return item > 2 }) // 打印 console.log(res)

find()

Array.prototype.myFind = function(callback) { // 判断参数是否是function,如果不是就抛出提示 if(typeof callback !== 'function') { throw 'argument must be a function' } // 循环遍历数组 this表示当前数组实例 for(let i = 0; i < this.length; i++) { // 得到当前遍历元素 const element = this[i] // 得到函数调用的返回值 const result = callback(element,i) // 一找到就返回该元素 if(result) return element } // 没有找到就返回undefined return undefined } // 定义数组 const arr = [55,22,11,33,44] // 使用 const res = arr.myFind((item,index)=> item===3) // 打印 console.log(res)

findIndex()

Array.prototype.myFindIndex = function(callback) { // 判断参数是否是function,如果不是就抛出提示 if(typeof callback !== 'function') { throw 'argument must be a function' } // 循环遍历数组 this表示当前数组实例 for(let i = 0; i < this.length; i++) { // 得到当前遍历元素 const element = this[i] // 得到函数调用的返回值 const result = callback(element,i) // 一找到就返回该元素的下表 if(result) return i } // 没有找到就返回-1 return -1 } // 定义数组 const arr = [55,22,11,33,44] // 使用 let res = arr.myFindIndex((item,index)=> item===3) // 打印 console.log(res)

every()

Array.prototype.myEvery = function(callback) { // 判断参数是否是function,如果不是就抛出提示 if(typeof callback !== 'function') { throw 'argument must be a function' } // 循环遍历数组 this表示当前数组实例 for(let i = 0; i < this.length; i++) { // 得到当前遍历元素 const element = this[i] // 得到函数调用的返回值 const result = callback(element,i) // 有一个false就返回false if(!result) return false } // 都是true返回true return true } // 定义数组 const arr = [55,22,11,33,44] // 使用 const res = arr.myEvery((item,index)=>{ return item > 0 }) // 打印 console.log(res)

some()

Array.prototype.mySome = function(callback) { // 判断参数是否是function,如果不是就抛出提示 if(typeof callback !== 'function') { throw 'argument must be a function' } // 循环遍历数组 this表示当前数组实例 for(let i = 0; i < this.length; i++) { // 得到当前遍历元素 const element = this[i] // 得到函数调用的返回值 const result = callback(element,i) // 如果有就返回true if(result) return true } // 都没有返回false return false } // 定义数组 const arr = [55,22,11,33,44] // 使用 const res = arr.mySome((item,index)=>{ return item = 2 }) // 打印 console.log(res)

forEach()

Array.prototype.myForEach = function(callback) { // 判断参数是否是function,如果不是就抛出提示 if(typeof callback !== 'function') { throw 'argument must be a function' } // 循环遍历数组 this表示当前数组实例 for(let i = 0; i < this.length; i++) { const element = this[i] callback(element,i) } } // 定义数组 const arr = [55,22,11,33,44] // 使用 arr.myForEach((item,index)=>{ console.log(item,index) }) // 打印 console.log(res)
最新回复(0)