2021/02/03 - JS数组的高阶函数的概念,以及有哪些常用的原生高阶函数
lxinr opened this issue · 0 comments
lxinr commented
高阶函数和普通函数其实是一样的,不过它有一个额外的能力就是
接受函数作为参数,或者返回值是函数
Array.prototype.map
map()方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果,因此它不会改变原数组
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
// Return element for new_array
}[, thisArg])
// thisArg - 执行 callback 函数时值被用作thisArray.prototype.filter
filter()方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素
var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
// thisArg - 执行 callback 函数时值被用作thisArray.prototype.reduce
reduce()方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
// initialValue - 作为第一次调用 callback函数时的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错Array.prototype.some()
some()方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值
arr.some(callback(element[, index[, array]])[, thisArg])