I'm currently using a code to analyze daily occurrences over the past 7 days. However, the code only captures days with occurrences and skips those without any data. I want to include all days in the analysis, even if they have a count of 0. Here's my current code:
var myObj;
fetch('https://blahblahblah?'+param1+param2+"FromDate"+"="+moment().subtract(7,'d').format('YYYY-MM-DD'))
.then(res=>res.json())
.then(data=>myObj= data);
var myRes= [];
myObj.forEach(function (elem) {
var date = elem.CreatedDate.split(' ')[0];
if (myRes[date]) {
myRes[date] += 1;
} else {
myRes[date] = 1;
}
});
The current output looks like this:
2020-12-11: 1
2020-12-12: 2
2020-12-13: 1
2020-12-15: 2
2020-12-16: 1
However, I would like the output to include days with 0 values, as shown below:
2020-12-10: 0
2020-12-11: 1
2020-12-12: 2
2020-12-13: 1
2020-12-14: 0
2020-12-15: 2
2020-12-16: 1
Your help will be greatly appreciated.
Edit: Here is a sample object data:
[{Id, Var1, Var2, CreationDate},
{1, 123, Var2, 2020-12-11},
{2, 1234, Var2, 2020-12-12},
{3, 12345, Var2, 2020-12-12},
{4, 1234, Var2, 2020-12-13},
{5, 321, Var2, 2020-12-15},
{6, 3214, Var2, 2020-12-15},
{7, 5432, Var2, 2020-12-16}]