I have the following array:
[
{category: 'Category 1', 'Apr 2021' : 10, 'Mar 2021' : 20, 'Feb 2021': 5},
{category: 'Category 2', 'Apr 2021' : 8, 'Mar 2021' : 2, 'Feb 2021': 15},
{category: 'Category 3', 'Apr 2021' : 7, 'Mar 2021' : 1, 'Feb 2021': 5}
]
I would like to add a total row with the sum of categories in the array.
Example:
[
{category: 'Category 1', 'Apr 2021' : 10, 'Mar 2021' : 20, 'Feb 2021': 5},
{category: 'Category 2', 'Apr 2021' : 8, 'Mar 2021' : 2, 'Feb 2021': 15},
{category: 'Category 3', 'Apr 2021' : 7, 'Mar 2021' : 1, 'Feb 2021': 5},
{category: 'Total', 'Apr 2021' : 25, 'Mar 2021' : 23, 'Feb 2021': 30}
]
I tried looping through all columns using Object.keys
. However, it feels like there may be an easier way to achieve this.
What is the best way to calculate the total row?
Please note that "Apr 2021", "Mar 2021" are not fixed so I won't be able to hardcode them.