[英]How to fill missing dates inside array of object using typescript/javascript?
I want to fill this object from today's date to next 7 days.我想从今天到接下来的 7 天填写这个 object。 Here is my object这是我的 object
let obj = {
"sessions": [{
"date": "15-05-2021"
},
{
"date": "16-05-2021"
},
{
"date": "18-05-2021"
}]
}
Expected output:预期 output:
let output = {
"sessions": [{
"date": "14-05-2021"
},
{
"date": "15-05-2021"
},
{
"date": "16-05-2021"
},
{
"date": "17-05-2021"
},
{
"date": "18-05-2021"
},
{
"date": "19-05-2021"
},
{
"date": "20-05-2021"
}]
}
Here is the code to generate array of dates from today to next 7 days这是生成从今天到接下来 7 天的日期数组的代码
function getWeekDates() {
let dates = [];
dates.push(new Date(Date.now() + 1000 * 3600 * 1).toLocaleDateString('en-GB').replace('/', '-').replace('/', '-'));
for (let i = 1; i <= 6; i++) {
dates.push(new Date(Date.now() + 1000 * 3600 * (i * 24)).toLocaleDateString('en-GB').replace('/', '-').replace('/', '-'));
}
console.log(dates);
}
getWeekDates();
//Output:["14-05-2021", "15-05-2021", "16-05-2021", "17-05-2021", "18-05-2021", "19-05-2021", "20-05-2021"]
I'm newbie so please be easy on me.我是新手,所以请对我放轻松。
Thanks in advance...提前致谢...
You can do it by replacing the obj.sessions with the newly generated dates.您可以通过用新生成的日期替换 obj.sessions 来做到这一点。
let obj = { "sessions": [{ "date": "15-05-2021" }, { "date": "16-05-2021" }, { "date": "18-05-2021" }] } function getWeekDates() { let dates = []; dates.push({ "date": new Date(Date.now() + 1000 * 3600 * 1).toLocaleDateString('en-GB').replace('/', '-').replace('/', '-')}); for (let i = 1; i <= 6; i++) { dates.push({ "date": new Date(Date.now() + 1000 * 3600 * (i * 24)).toLocaleDateString('en-GB').replace('/', '-').replace('/', '-')}); } obj.sessions = [...dates] console.log(obj) } getWeekDates();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.