DEV Community

saifullah008
saifullah008

Posted on

Need help

let arr=[{name:'saif',count:1},{name:'harsh',count:3}

how can I find total count (i.e, 1 + 3 = 4) ?

Top comments (2)

Collapse
 
link2twenty profile image
Andrew Bone

It feels like this might be for an assignment.

const arr = [{
  name: 'saif',
  count: 1
}, {
  name: 'harsh',
  count: 3
}]

const count = arr.reduce((c, a) => c + a.count, 0)
Enter fullscreen mode Exit fullscreen mode

I'd recommend reading about it here

Collapse
 
vikirobles profile image
Vicky Vasilopoulou

const result = array.map((item) => item.count).reduce((acc, total) => acc + total)

Enter fullscreen mode Exit fullscreen mode