I need to populate this object with dates starting from today up to the next 7 days. Here is my current object:
let obj = {
"sessions": [{
"id": 0,
"available_capacity": 3,
"date": "15-05-2021"
},
{
"id": 1,
"available_capacity": 5,
"date": "16-05-2021"
},
{
"id": 2,
"available_capacity": 2,
"date": "18-05-2021"
}]
}
Desired output:
let output = {
"sessions": [{
"date": "14-05-2021"
},
{
"id": 0,
"available_capacity": 3,
"date": "15-05-2021"
},
{
"id": 1,
"available_capacity": 5,
"date": "16-05-2021"
},
{
"date": "17-05-2021"
},
{
"id": 2,
"available_capacity": 2,
"date": "18-05-2021"
},
{
"date": "19-05-2021"
},
{
"date": "20-05-2021"
}]
}
Below is a function that generates an array of dates for the coming week:
function getWeekDates() {
let dates = [];
for (let i = 0; i <= 6; i++) {
dates.push(new Date(Date.now() + 1000 * 3600 * (i * 24)).toLocaleDateString('en-GB').replace('/', '-').replace('/', '-'));
}
console.log(dates);
}
getWeekDates();
//result: ["14-05-2021", "15-05-2021", "16-05-2021", "17-05-2021", "18-05-2021", "19-05-2021", "20-05-2021"]
How can I add the missing dates to my object?