I am in need of the count for the current week only. Specifically, I want to calculate monthly totals with a breakdown per week. Currently, I can retrieve the weekly count for each month. Now, my goal is to display only the existing weeks. For example, if May has values for week 1 and week 2, I want to see the counts only for week 1 and week 2 under the month of May. Similarly, for July, I only want to display the counts for week 1 and week 3.
const response=[
{
"UserName": "User1",
"week": "Week 1",
"Type": "type3",
"months": "May",
"count": 2
},
{
"UserName": "User1",
"week": "Week 2",
"Type": "type1",
"months": "Jun",
"count": 1
},
...
];
const WEEKS = ["Week 1", "Week 2", "Week 3", "Week 4"];
const result = response.reduce((acc, obj) => {
existingObj = acc.find(ele => ele.UserName == obj.UserName && ele.month == obj.months && ele.Type == obj.Type)
if (existingObj) {
...
} else {
...
}
return acc;
}, []);
console.log(result);
Expected Output:
[{"UserName":"user1","month":"May","Type":"Type3","Week1":2,"Week3":3},
{"UserName":"user2","month":"May","Type":"Type3","Week1":2},
...
]