1.Reduce
过程是一个函数 pipeline,参数组合+函数pipeline
JavaScript 函数式编程实践指南 - 修言 - 掘金小册
const arr = [1, 2, 3] const initialValue = 0 const add = (previousValue, currentValue) =>previousValue+currentValue;
//执行顺序 0 + 1 + 2 + 3
const sumArr = arr.reduce(add,0);
console.log(sumArr) //expected output: 6
执行过程:
2.map
function add1AndPush(previousValue, currentValue) { // previousValue 是一个数组 previousValue.push(currentValue + 1) return previousValue } const arr = [1,2,3] const newArray = arr.map((num)=> num+1) const newArray1 = arr.reduce(add1AndPush,[]) console.log(newArray);//[2, 3, 4] console.log(newArray1);//[2, 3, 4]
3.compose
4.pipe
标签:arr,previousValue,const,函数,JavaScript,currentValue,编程 From: https://www.cnblogs.com/xm123/p/18556843