檔案:
"notifcations": [
{
"title": "Joined the Tournament.",
"description": "You have registered in the Tournament of Tournament #142, issued ?10 from your balance.",
"amount": 10,
"createdAt": "2022-05-08T12:03:02.431Z",
"expiresAt": "2022-05-09T12:03:05.983Z",
"_id": "6277b17968729c2811137fb0"
},
{
"title": "Joined the Tournament.",
"description": "You have registered in the Tournament of Tournament #143, issued ?10 from your balance.",
"amount": 10,
"createdAt": "2022-05-08T12:03:02.431Z",
"expiresAt": "2022-05-09T12:03:05.983Z",
"_id": "6277b17968729c2811137fb0"
}
]
在這里,在通知陣列中,我想洗掉所有過期的物件 - 物件中的“expiresAt”。喜歡洗掉所有物件expiresAt < Date.now()
通知陣列存盤在 mongodb 中,我想為此創建一個函式來洗掉過期的通知。
任何幫助請!?
uj5u.com熱心網友回復:
可能最簡單的方法是使用Array.filter()
const myObj = {
"notifcations": [
{
"title": "Joined the Tournament.",
"description": "You have registered in the Tournament of Tournament #141, issued ?10 from your balance.",
"amount": 10,
"createdAt": "2021-05-08T12:03:02.431Z",
"expiresAt": "2021-05-08T12:03:05.983Z",
"_id": "6277b17968729c2811137fb0"
},
{
"title": "Joined the Tournament.",
"description": "You have registered in the Tournament of Tournament #142, issued ?10 from your balance.",
"amount": 10,
"createdAt": "2022-05-08T12:03:02.431Z",
"expiresAt": "2022-05-09T12:03:05.983Z",
"_id": "6277b17968729c2811137fb0"
},
{
"title": "Joined the Tournament.",
"description": "You have registered in the Tournament of Tournament #143, issued ?10 from your balance.",
"amount": 10,
"createdAt": "2022-05-08T12:03:02.431Z",
"expiresAt": "2022-05-09T12:03:05.983Z",
"_id": "6277b17968729c2811137fb0"
}
]
};
myObj.notifcations = myObj.notifcations.filter(a => new Date(a.expiresAt) > new Date());
console.log(myObj);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/470974.html
標籤:javascript 节点.js mongodb 猫鼬
