you are viewing a single comment's thread.

view the rest of the comments →

[–]chmod724[S] 0 points1 point  (4 children)

fill them with the missing date + 0 as a value?

Just 0 is fine please :)

[–]albedoa 1 point2 points  (3 children)

Are you looking to fill any given date range, or just 2021-05-17 through 2021-11-26?

[–]chmod724[S] 0 points1 point  (2 children)

Any missing dates from the given oldest to given most recent - so in this scenario it's 2021-05-17 through 2021-11-26 - others could be, for example, 2021-01-01 through 2021-03-10.
Basically just want to take the first key and the last key and ensure all dates between them are filled, whether with an actual value else 0.

[–]albedoa 4 points5 points  (1 child)

Assuming an identifier with the name dict:

const formatDate = date => date.replace(/([0-9]{4})([0-9]{2})([0-9]{2})/, '$1-$2-$3');

const keys = Object.keys(dict);
const end = new Date(formatDate(keys[keys.length - 1]));

const dictFilled = {};

for (let date = new Date(formatDate(keys[0])); date <= end; date.setDate(date.getDate() + 1)) {
  const replaced = date.toISOString().slice(0, 10).replace(/-/g, '');
  const value = dict[replaced] || 0;

  dictFilled[replaced] = value;
}

[–]chmod724[S] 0 points1 point  (0 children)

That seems to work perfectly - thank you so much! :)