-2

I have a shopping cart in one of my projects about e-commerces, and I want to show in the side of the cart icon the number of items on it. There's so many ways to make it, but honestly my brain is so blocked right now, and I need help...

enter image description here

I thought about looping through the array to get the "cant" key value of each object within it and then adding them together to display them next to the shopping cart, but I still don't know how to do it yet...

Alan
  • 47
  • 7

2 Answers2

1

You can use the reduce method.

const totalCount = items.reduce((total, currentItem) => (
   total + currentItem.cant
), 0)
justMatys
  • 112
  • 3
0

You just got to loop through and keep a counter.

var counter = 0;
cartArray.forEach(i => {
   counter += i.cant;
});
return counter;
Dropout
  • 13,653
  • 10
  • 56
  • 109