我有一个带有日期键的对象数组,我想过滤掉具有当前周的对象,我怎样才能实现这个返回我当前周的对象。我尝试使用过滤器但首先我需要格式化日期我猜是关键。
const notificationItems = [
{
id: 1,
date: '2021-02-10T05:04:59.525589Z',
name: 'Glenn',
},
{
id: 2,
date: '2021-02-10T05:04:59.525589Z',
name: 'Root',
},
{
id: 3,
date: '2021-01-08T05:04:59.525589Z',
name: 'Smith',
},
{
id: 4,
date: '2019-02-03T05:04:59.525589Z',
name: 'Ryan',
},
{
id: 5,
date: '2019-01-03T05:04:59.525589Z',
name: 'Bob',
}
]
最佳答案
也许你可以考虑使用 moment.js 库 https://github.com/moment/moment
notificationItems.filter(
(notification) =>
moment(notification.date).week() === moment().week() &&
moment(notification.date).year() === moment().year()
);
然后通过这段代码你将只收到本周的通知
关于javascript - 如何从javascript中给定的对象日期数组获取当前周,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66131919/