const average = data=>data.map((item, idx, origin)=>Math.round(origin.slice(0,idx+1).reduce((a, b)=>a + b, 0) / (idx + 1))); 1.用map遍历求值2.map中的第三个参数origin可以放回当前的值3.reduce((a+b)=>a+b,0)当a为undefined,0参数赋值给a
时间: 2024-10-13 11:57:40
const average = data=>data.map((item, idx, origin)=>Math.round(origin.slice(0,idx+1).reduce((a, b)=>a + b, 0) / (idx + 1))); 1.用map遍历求值2.map中的第三个参数origin可以放回当前的值3.reduce((a+b)=>a+b,0)当a为undefined,0参数赋值给a