手写filter办法
filter()办法返回一个数组,返回的每一项是在回调函数中执行后果true。
const arr = [1,2,3,4,5]const filterArr = arr.filter(item=>item>2)console.log(filterArr)//[3, 4, 5]
手写map办法
map()办法依据回调函数映射一个新数组
const arr = [1,2,3,4,5]const mapArr = arr.map(item=>item*2)console.log(mapArr)//[2, 4, 6, 8, 10]
手写reduce办法
reduce()办法循环迭代,回调函数的后果都会作为下一次的形参的第一个参数。
const arr =[1,2,3,4,5]const reduceArr = arr.reduce((a,b)=>a+b,2)console.log(reduceArr)//15
手写every办法
every() 办法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。
const arr = [1,2,3,4,5]const everyArr = arr.every(item=>item>0)console.log(everyArr)//true
手写some办法
some() 办法测试数组中是不是至多有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。
const arr=[1,2,3,4,5] const someArr = arr.some(item=>item>3)console.log(someArr)// true
手写find办法
find() 办法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。
const arr=[1,2,3,4,5] const findArr = arr.find(item=>item>1)console.log(findArr)// 2