Skip to content
Advertisement

How to add the values ​in an array of objects depending on the date value of each object

I have this array:

JavaScript

I want to add the distance and time values ​​grouping them by the month indicated by the start_date value. For example, if two start_dates have the same month 2022-12-01 and 2022-12-08, how can I add the distance and time values ​​of those two months?

so i get a new array like this:

JavaScript

Advertisement

Answer

you can use reduce to group them by month which will give an object like

JavaScript

and using Object.values get the values array of it

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement