Doc:
"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" } ] Aquí, en la matriz de notificaciones, quiero eliminar todos los objetos que están vencidos: "expiresAt" en el objeto. Me gusta eliminar todos los objetos en los que expiresAt < Date.now()
La matriz de notificaciones está almacenada en mongodb y quiero crear una función para eliminar las notificaciones caducadas.
¿Alguna ayuda por favor?
Probablemente el método más simple sería usar 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);