好了,他们最简单而不装逼的解释就是:
map:
[x1, x2, x3, x4].map(f) = [f(x1),f(x2),f(x3),f(x4)]
reduce :
[x1, x2, x3, x4].reduce(f) = f(f(f(x1, x2), x3), x4)
代码示例,比如求数组的每一个元素的平方和将数组转成字符串(中间无逗号)
function f(x){
return x*x
}
function r(x,y){
return x.toString()+y.toString()
}
let a = [1,2,3,4,5]
let arr = a.map(f)//注意array是一个数组
let str = a.reduce(r)//注意str是一个字符串
console.log(arr);//[1,4,9,16,25]
console.log(str);//"12345"