The counting method used - the sum of the array of objects

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. traverse the sum

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

 

Guess you like

Origin www.cnblogs.com/freefy/p/11111550.html
Recommended