I have got a data sample:
{
data: [
{ day: 0, amount: 0 },
{ day: 3, amount: 5 },
{ day: 7, amount: 8 },
{ day: 8, amount: 14 }
]
}
For doing some math and calculating local regressions I need to fill up each missing day, while I know the total amount of days.
Lets say there are 10 days with the example above, this should be my outcome:
{
data: [
{ day: 0, amount: 0 },
{ day: 1, amount: 0 },
{ day: 2, amount: 0 },
{ day: 3, amount: 5 },
{ day: 4, amount: 5 },
{ day: 5, amount: 5 },
{ day: 6, amount: 5 },
{ day: 7, amount: 8 },
{ day: 8, amount: 14 }
{ day: 9, amount: 14 }
]
}
How can I achieve this in style? (optional with ES6 syntax)
My own guess seems to be quite complicated:
First I could create an array with each day:
const amountOfTotalDays = 10;
const daysByAmount = [
{ day: 0, amount: 0 },
{ day: 3, amount: 5 },
{ day: 7, amount: 8 },
{ day: 8, amount: 14 }
];
const daysHelper = [...Array(amountOfTotalDays).keys()];
Then I could loop over each entry and check for the correct amount. Maybe like this:
const daysByAmountUpfilled = [];
daysHelper.map(d => {
const existingDay = daysByAmount.findIndex(entry => entry.day === d);
if(existingDay >= 0) {
daysByAmountUpfilled.push(
daysByAmount[existingDay]
);
} else {
// find previous entry closest to this day
// but I do not know how
}
});