js reduce用法

let books = [
0,
{bookName:"python",price:10,count:1},
{bookName:"Ruby",count:2,price:20},
{bookName:"Java",count:4,price:30},
{bookName:"php",count:2,price:40},
{bookName:"Go",count:1,price:50},
];
// pre 代表第一项, next代码第二项,
// 第二次循环,pre为undefined next为第三项
// return 结果是下一次next的值
let sum = books.reduce(function (prev,next,index,item) {
return prev+(next.price* next.count);
});



console.log(sum);
posted @ 2019-07-23 15:03  雷神约  阅读(164)  评论(0编辑  收藏  举报