我有一组日期戳:let dateList = [ "2019-07-22T08:40:33.537Z", "2019-07-22T08:40:33.537Z", "2019-07-23T12:02:53.143Z", "2019-07-23T12:02:53.143Z", "2019-07-23T12:12:00.434Z", "2019-07-23T12:12:54.649Z", "2019-07-23T12:14:57.547Z", "2019-07-23T12:14:57.547Z", "2019-07-23T12:16:45.011Z", "2019-07-23T12:16:45.011Z", "2019-07-23T12:21:20.527Z", "2019-07-23T12:24:30.095Z", "2019-07-23T12:25:33.251Z", "2019-07-23T12:27:53.809Z", "2019-07-23T12:27:53.809Z" ]我需要从中获取唯一日期的列表。以下功能允许我删除重复项。let uniqueDates = [...new Set(dateList)];但是,它仍然是一组独特的时间戳。我真正需要的是一组独特的日期。所以,如果有两个相同日期的时间戳,我只需要唯一数组中的一个。你能帮助我如何实现这一目标吗?
1 回答
慕村9548890
TA贡献1884条经验 获得超4个赞
将 映射dateList为仅包含日期(删除时间部分):
let dateList = [ "2019-07-22T08:40:33.537Z", "2019-07-22T08:40:33.537Z", "2019-07-23T12:02:53.143Z", "2019-07-23T12:02:53.143Z", "2019-07-23T12:12:00.434Z", "2019-07-23T12:12:54.649Z", "2019-07-23T12:14:57.547Z", "2019-07-23T12:14:57.547Z", "2019-07-23T12:16:45.011Z", "2019-07-23T12:16:45.011Z", "2019-07-23T12:21:20.527Z", "2019-07-23T12:24:30.095Z", "2019-07-23T12:25:33.251Z", "2019-07-23T12:27:53.809Z", "2019-07-23T12:27:53.809Z" ]
const uniqueDates = [...new Set(
dateList
.map((str) => str.match(/^\d{4}-\d{2}-\d{2}/)[0])
)];
console.log(uniqueDates);
添加回答
举报
0/150
提交
取消