JavaScript中分别用数组forEach()、reduce()方法求数组元素的和

function sum(arr) {
    let count=0;
    arr.forEach(function(x){
        count+=x;
    })
    return count;
}

function sum(arr) {
    return arr.reduce((a,b)=>a+b,0);
}

原文地址:https://www.cnblogs.com/WP-WangPin/p/13913885.html