JavaScript reduce() 方法-计算数组元素相加后的总和
计算数组元素相加后的总和:
var numbers = [65, 44, 12, 4]; function getSum(total, num) { return total + num; } function myFunction(item) { document.getElementById("demo").innerHTML=numbers.reduce(getSum); }
输出结果:
125
计算数组元素相加后的总和:
var numbers = [65, 44, 12, 4]; function getSum(total, num) { return total + num; } function myFunction(item) { document.getElementById("demo").innerHTML=numbers.reduce(getSum); }
输出结果: