How can I check if the every department’s months is existing while comparing it to themonthsName array using startMonth and endMonth`
I’m sorry I can’t explain the words properly, This is my alternative to my previous question How can I return it as a 0 instead of skipping the month in SQL?
This is the code I run after getting the result to the database, it simply group the deparment’s event_count by department and by month.
function groupDataByDepartment(results, startMonth, endMonth) {
const monthNames = [
{ abbreviation: "Jan", number: 1 },
{ abbreviation: "Feb", number: 2 },
{ abbreviation: "Mar", number: 3 },
{ abbreviation: "Apr", number: 4 },
{ abbreviation: "May", number: 5 },
{ abbreviation: "Jun", number: 6 },
{ abbreviation: "Jul", number: 7 },
{ abbreviation: "Aug", number: 8 },
{ abbreviation: "Sep", number: 9 },
{ abbreviation: "Oct", number: 10 },
{ abbreviation: "Nov", number: 11 },
{ abbreviation: "Dec", number: 12 },
];
const groupedData = {};
let currentID = 1; // Initialize ID counter
let currentDepartment;
results.forEach((entry) => {
const { department, year, month, event_count } = entry;
if (department !== currentDepartment) {
currentDepartment = department;
currentID = 1; // Reset ID counter when department changes
}
if (!groupedData[department]) {
groupedData[department] = [];
}
groupedData[department].push({
id: currentID++,
[month]: event_count,
year: year,
});
});
return groupedData;
}
const groupedData = groupDataByDepartment(sampleResult, 12, 1)
Now the result will be like this
sampleResult = [
{
"department": "Department 1",
"year": 2022,
"month": "Dec",
"event_count": 36
},
{
"department": "Department 1",
"year": 2023,
"month": "Jan",
"event_count": 28
},
{
"department": "Department 2",
"year": 2022,
"month": "Dec",
"event_count": 8
},
{
"department": "Department 2",
"year": 2023,
"month": "Jan",
"event_count": 50
},
{
"department": "Department 3",
"year": 2022,
"month": "Dec",
"event_count": 1
},
{
"department": "Department 3",
"year": 2023,
"month": "Jan",
"event_count": 34
},
{
"department": "Department 4",
"year": 2023,
"month": "Jan",
"event_count": 11
},
{
"department": "Department 5",
"year": 2022,
"month": "Dec",
"event_count": 2
},
{
"department": "Department 5",
"year": 2023,
"month": "Jan",
"event_count": 21
},
{
"department": "Department 6",
"year": 2022,
"month": "Dec",
"event_count": 17
},
{
"department": "Department 6",
"year": 2023,
"month": "Jan",
"event_count": 72
},
{
"department": "Deparment 7",
"year": 2022,
"month": "Dec",
"event_count": 38
},
{
"department": "Deparment 7",
"year": 2023,
"month": "Jan",
"event_count": 14
},
{
"department": "Department 8",
"year": 2022,
"month": "Dec",
"event_count": 44
},
{
"department": "Department 8",
"year": 2023,
"month": "Jan",
"event_count": 132
}
]
What i’m trying to achieve is with the use of startMonth and endMonth. I want to loop and compare the monthsName and the sampleResult and if the month is not existing, still input the month its misssing but make the result as 0
startMonth = 12
endMonth = 1
So in the sampleResult in the Department 4. The function skip the month of Dec, because I don’t have any data during Dec, instead of leaving it blank, I want it to be like this.
sampleResult = [
...other results
{
"department": "Department 4",
"year": 2022,
"month": "Dec",
"event_count": 0
},
{
"department": "Department 4",
"year": 2023,
"month": "Jan",
"event_count": 11
},
]