如何根据猫鼬的到期时间从数组中删除整个对象?
How can I remove an entire object from an array by its expiry time in mongoose?
文档:
"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" in object.想删除所有 expiresAt < Date.now()
的对象
通知数组存储在 mongodb 中,我想为此创建一个函数来删除过期的通知。
请帮忙!?
可能最简单的方法是使用 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);
文档:
"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" in object.想删除所有 expiresAt < Date.now()
通知数组存储在 mongodb 中,我想为此创建一个函数来删除过期的通知。
请帮忙!?
可能最简单的方法是使用 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);