-1

I have an array of objects that contains categories and points. I want to sum each category's points.

   {
     category: A,
     points:2
    },
    {
      category: A
      points: 3
    },
    {
     category: B,
     points:2
    },
    {
      category: B
      points: 3
    }
   ]

How can I sum points from category A and points from category B in the slickest way possible ?

Andre
  • 11
  • 4
  • Can you share what have you tried? – Hemant Parashar Mar 07 '21 at 11:45
  • Welcome to Stack Overflow! Please take the [tour] (you get a badge!) and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Mar 07 '21 at 11:51
  • @Andre - How utterly bizarre. I saw [tag:java] but...SO would say if it had been edited. Sorry about that. :-) – T.J. Crowder Mar 07 '21 at 17:23

1 Answers1

0

I think this code help you

arr = [{
         category: 'A',
         points:2
        },
        {
          category: 'A',
          points: 3
        },
        {
         category: 'B',
         points:2
        },
        {
          category: 'B',
          points: 5
        }]
    
    let total = 0;
     arr.filter(function(item){
            return total += item['points']
        });

console.log(total)
Eahiya
  • 931
  • 1
  • 6
  • 12