After receiving a list of dates from an AJAX request, I am aiming to generate an array containing the number of orders placed for each month. This data will be used to create charts using ChartJS.
For example, if there were 12 orders made in January, the array would have the number 12 stored at index 0.
The issue lies in my current code structure; it appears messy and inefficient, especially when considering expanding it to track daily orders with multiple switch cases.
Although the existing code is functional, I am seeking a more streamlined and efficient approach.
// Array storing order quantity per month
var monthsArray = [0,0,0,0,0,0,0,0,0,0,0,0];
for(var i in data) {
// Creating a new Date object from each orderDate
var originalDate = data[i].orderDate;
var myDate = new Date(Date.parse(originalDate));
var monthIndex = myDate.getMonth();
// Incrementing the value at the corresponding index in the array based on the month number
meses[monthIndex] += 1;
}