对数组中每个对象的某个值进行求和
数组求和,reduce方法:
let total = 0;
let Array = [1, 2, 3, 4, 5];
total = Array.reduce((prev, next) => {
return prev + next;
}, 0);
console.log(total);
数组对象中的某个值求和,reduce方法:
let PriceTotal = 0;
let ArrayList = [{
pricenum: 80
},{
pricenum: 90
},{
pricenum: 100
}];
// 默认传如一个参数0,prev的第一项就是0,每次加上下一次的价格即可
PriceTotal = ArrayList.reduce((prev, next) => {
return prev + next.price;
}, 0);