常用的计数方法--数组对象总和

1.使用reduce

let arr = [{name:'a',count:23},{name:'b',count:23},{name:'c',count:23},{name:'d',count:23},{name:'e',count:23}]
let counts = arr.reduce(function(prev,item){
  return prev+item.count
},0)

2.遍历相加

let count = 0;
arr.forEach(el=>{
  count+=el.count
})

 

posted @ 2019-06-30 22:31  StupidTom  阅读(579)  评论(0编辑  收藏  举报