let sum = (...rest)=> rest.reduce((x,y)=>x+y,0)
注意reduce的第二参数必须要为零,不填会报错。
语法
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
reduce 为数组中的每一个元素依次执行回调函数,不包括数组中被删除或从未被赋值的元素,接受四个参数:初始值(或者上一次回调函数的返回值),当前元素值,当前索引,调用 reduce 的数组。
- callback (执行数组中每个值的函数,包含四个参数)
- accumulator (上一次调用回调返回的值,或者是提供的初始值(initialValue)
- currentValue (数组中当前被处理的元素)
- index (当前元素在数组中的索引)
- array (调用 reduce 的数组)
- initialValue (作为第一次调用 callback 的第一个参数。)