I have an array generated through a code where I'm calculating, for each day from now to 7 days ago, the daily occurrences. Right now my code is skipping the days where there aren't occurrences, but I would like to insert those days together with a 0 count value. Here's my 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;
}
});
Right now I'm getting a similar result:
2020-12-11: 1
2020-12-12: 2
2020-12-13: 1
2020-12-15: 2
2020-12-16: 1
Because on 12-10 I didn't have any value and on 12-14 neither. Given the startTime moment().subtract(7,'d').format('YYYY-MM-DD')
how can I output the days with 0 values like in the format 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
Thank you very much
Edit: here's my obj:
[{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}]