I'm trying to find the total amount group by data attribute name from a list using jQuery. Similar to grouping categories and find the sum.
For example, I would like to create something like this:
group1 = 3
group2 = 5
group3 = 3
from this list:
<span class="data-center" data-category="group1" data-amount="1"></span>
<span class="data-center" data-category="group2" data-amount="1"></span>
<span class="data-center" data-category="group2" data-amount="2"></span>
<span class="data-center" data-category="group1" data-amount="2"></span>
<span class="data-center" data-category="group2" data-amount="2"></span>
<span class="data-center" data-category="group3" data-amount="3"></span>
I wonder if there is some jQuery function help to achieve it, something like groupByName()
?