我有一个多维数组的对象,我想过滤如下所述:

let participants =
[
   {
      "participant_id": 2,
      "name": "Bond James",
      "allschedules": [
         {
            "schedule_id": 1,
            "name": "James_Bond_SIL_January_22 (AM) - 07:00 - 15:00",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 31,
            "start_date": "2022-01-01 07:00:00",
            "end_date": "2022-01-31 15:00:00",
            "shifts": [
               "2022-01-01 07:00:00",
               "2022-01-02 07:00:00",
               "2022-01-03 07:00:00",
               "2022-01-04 07:00:00"
            ]
         },
         {
            "schedule_id": 2,
            "name": "Bonds-1:2 Schedule",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 5,
            "start_date": "2022-01-12 07:00:00",
            "end_date": "2022-01-16 11:00:00",
            "shifts": [
               "2022-01-12 07:00:00",
               "2022-01-13 07:00:00",
               "2022-01-14 07:00:00",
               "2022-01-15 07:00:00",
               "2022-01-16 07:00:00"
            ]
         },
         {
            "schedule_id": 9,
            "name": "test april",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 4,
            "start_date": "2022-04-09 16:00:00",
            "end_date": "2022-04-12 20:00:00",
            "shifts": [
               "2022-04-09 16:00:00",
               "2022-04-10 16:00:00",
               "2022-04-11 16:00:00",
               "2022-04-12 16:00:00"
            ]
         },
         {
            "schedule_id": 10,
            "name": "CP - James Bond",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 10,
            "start_date": "2022-04-11 07:00:00",
            "end_date": "2022-05-11 09:00:00",
            "shifts": [
               "2022-04-11 07:00:00",
               "2022-04-13 07:00:00",
               "2022-04-18 07:00:00",
               "2022-04-20 07:00:00",
               "2022-04-25 07:00:00",
               "2022-04-27 07:00:00",
               "2022-05-02 07:00:00",
               "2022-05-04 07:00:00",
               "2022-05-09 07:00:00",
               "2022-05-11 07:00:00"
            ]
         },
         {
            "schedule_id": 11,
            "name": "James_Miller_App_Test",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 30,
            "start_date": "2022-04-21 07:00:00",
            "end_date": "2022-05-20 15:00:00",
            "shifts": [
               "2022-04-30 07:00:00",
               "2022-05-01 07:00:00",
               "2022-05-02 07:00:00",
               "2022-05-03 07:00:00",
               "2022-05-04 07:00:00",
               "2022-05-05 07:00:00",
               "2022-05-06 07:00:00",
               "2022-05-07 07:00:00",
               "2022-05-08 07:00:00",
               "2022-05-09 07:00:00",
               "2022-05-10 07:00:00",
               "2022-05-11 07:00:00",
               "2022-05-12 07:00:00",
               "2022-05-13 07:00:00",
               "2022-05-14 07:00:00",
               "2022-05-15 07:00:00",
               "2022-05-16 07:00:00",
               "2022-05-17 07:00:00",
               "2022-05-18 07:00:00",
               "2022-05-19 07:00:00",
               "2022-05-20 07:00:00"
            ]
         },
         {
            "schedule_id": 12,
            "name": "Grouped Schedule for April 2022",
            "schedule_type": "Shared/Grouped Schedule",
            "number_of_shifts": 3,
            "start_date": "2022-04-28 12:00:00",
            "end_date": "2022-04-30 18:00:00",
            "shifts": [
               "2022-04-28 12:00:00",
               "2022-04-29 12:00:00",
               "2022-04-30 12:00:00"
            ]
         }
      ]
   },
   {
      "participant_id": 3,
      "name": "Barkley Charles",
      "allschedules": [
         {
            "schedule_id": 12,
            "name": "Grouped Schedule for April 2022",
            "schedule_type": "Shared/Grouped Schedule",
            "number_of_shifts": 3,
            "start_date": "2022-04-28 12:00:00",
            "end_date": "2022-04-30 18:00:00",
            "shifts": [
               "2022-04-28 12:00:00",
               "2022-04-29 12:00:00",
               "2022-04-30 12:00:00"
            ]
         }
      ]
   }
]

从上面的数组中,我有一个名为:allschedules的对象,它有自己的对象.在allschedules中感兴趣的对象是:shifts,它有一个日期array.目标是只返回满足条件的participants,其中shifts数组中的日期是当前月份的日期.

这就是我所做的:

participants.filter((participant) => {
        return participant.allschedules.filter((schedule) => {
          schedule.shifts.find(
            (shift) => moment(shift).format("M") == moment().format("M")
          );
        });
      })

但这会从定义的participants变量返回完全相同的对象.所以什么都不过滤.正确的方法是什么?

let participants =
[
   {
      "participant_id": 2,
      "name": "Bond James",
      "allschedules": [
         {
            "schedule_id": 1,
            "name": "James_Bond_SIL_January_22 (AM) - 07:00 - 15:00",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 31,
            "start_date": "2022-01-01 07:00:00",
            "end_date": "2022-01-31 15:00:00",
            "shifts": [
               "2022-01-01 07:00:00",
               "2022-01-02 07:00:00",
               "2022-01-03 07:00:00",
               "2022-01-04 07:00:00"
            ]
         },
         {
            "schedule_id": 2,
            "name": "Bonds-1:2 Schedule",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 5,
            "start_date": "2022-01-12 07:00:00",
            "end_date": "2022-01-16 11:00:00",
            "shifts": [
               "2022-01-12 07:00:00",
               "2022-01-13 07:00:00",
               "2022-01-14 07:00:00",
               "2022-01-15 07:00:00",
               "2022-01-16 07:00:00"
            ]
         },
         {
            "schedule_id": 9,
            "name": "test april",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 4,
            "start_date": "2022-04-09 16:00:00",
            "end_date": "2022-04-12 20:00:00",
            "shifts": [
               "2022-04-09 16:00:00",
               "2022-04-10 16:00:00",
               "2022-04-11 16:00:00",
               "2022-04-12 16:00:00"
            ]
         },
         {
            "schedule_id": 10,
            "name": "CP - James Bond",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 10,
            "start_date": "2022-04-11 07:00:00",
            "end_date": "2022-05-11 09:00:00",
            "shifts": [
               "2022-04-11 07:00:00",
               "2022-04-13 07:00:00",
               "2022-04-18 07:00:00",
               "2022-04-20 07:00:00",
               "2022-04-25 07:00:00",
               "2022-04-27 07:00:00",
               "2022-05-02 07:00:00",
               "2022-05-04 07:00:00",
               "2022-05-09 07:00:00",
               "2022-05-11 07:00:00"
            ]
         },
         {
            "schedule_id": 11,
            "name": "James_Miller_App_Test",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 30,
            "start_date": "2022-04-21 07:00:00",
            "end_date": "2022-05-20 15:00:00",
            "shifts": [
               "2022-04-30 07:00:00",
               "2022-05-01 07:00:00",
               "2022-05-02 07:00:00",
               "2022-05-03 07:00:00",
               "2022-05-04 07:00:00",
               "2022-05-05 07:00:00",
               "2022-05-06 07:00:00",
               "2022-05-07 07:00:00",
               "2022-05-08 07:00:00",
               "2022-05-09 07:00:00",
               "2022-05-10 07:00:00",
               "2022-05-11 07:00:00",
               "2022-05-12 07:00:00",
               "2022-05-13 07:00:00",
               "2022-05-14 07:00:00",
               "2022-05-15 07:00:00",
               "2022-05-16 07:00:00",
               "2022-05-17 07:00:00",
               "2022-05-18 07:00:00",
               "2022-05-19 07:00:00",
               "2022-05-20 07:00:00"
            ]
         },
         {
            "schedule_id": 12,
            "name": "Grouped Schedule for April 2022",
            "schedule_type": "Shared/Grouped Schedule",
            "number_of_shifts": 3,
            "start_date": "2022-04-28 12:00:00",
            "end_date": "2022-04-30 18:00:00",
            "shifts": [
               "2022-04-28 12:00:00",
               "2022-04-29 12:00:00",
               "2022-04-30 12:00:00"
            ]
         }
      ]
   },
   {
      "participant_id": 3,
      "name": "Barkley Charles",
      "allschedules": [
         {
            "schedule_id": 12,
            "name": "Grouped Schedule for April 2022",
            "schedule_type": "Shared/Grouped Schedule",
            "number_of_shifts": 3,
            "start_date": "2022-04-28 12:00:00",
            "end_date": "2022-04-30 18:00:00",
            "shifts": [
               "2022-04-28 12:00:00",
               "2022-04-29 12:00:00",
               "2022-04-30 12:00:00"
            ]
         }
      ]
   }
]

console.log(
      participants.filter((participant) => {
        return participant.allschedules.filter((schedule) => {
          schedule.shifts.find(
            (shift) => moment(shift).format("M") == moment().format("M")
          );
        });
      })
    )
.as-console-wrapper { max-height: 100% !important; top: 0 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

我想要的过滤数组(按当月过滤)如下:

[
   {
      "participant_id": 2,
      "name": "Bond James",
      "allschedules": [
         {
            "schedule_id": 10,
            "name": "CP - James Bond",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 10,
            "start_date": "2022-04-11 07:00:00",
            "end_date": "2022-05-11 09:00:00",
            "shifts": [
               "2022-04-11 07:00:00",
               "2022-04-13 07:00:00",
               "2022-04-18 07:00:00",
               "2022-04-20 07:00:00",
               "2022-04-25 07:00:00",
               "2022-04-27 07:00:00",
               "2022-05-02 07:00:00",
               "2022-05-04 07:00:00",
               "2022-05-09 07:00:00",
               "2022-05-11 07:00:00"
            ]
         },
         {
            "schedule_id": 11,
            "name": "James_Miller_App_Test",
            "schedule_type": "Individual Schedule",
            "number_of_shifts": 30,
            "start_date": "2022-04-21 07:00:00",
            "end_date": "2022-05-20 15:00:00",
            "shifts": [
               "2022-04-30 07:00:00",
               "2022-05-01 07:00:00",
               "2022-05-02 07:00:00",
               "2022-05-03 07:00:00",
               "2022-05-04 07:00:00",
               "2022-05-05 07:00:00",
               "2022-05-06 07:00:00",
               "2022-05-07 07:00:00",
               "2022-05-08 07:00:00",
               "2022-05-09 07:00:00",
               "2022-05-10 07:00:00",
               "2022-05-11 07:00:00",
               "2022-05-12 07:00:00",
               "2022-05-13 07:00:00",
               "2022-05-14 07:00:00",
               "2022-05-15 07:00:00",
               "2022-05-16 07:00:00",
               "2022-05-17 07:00:00",
               "2022-05-18 07:00:00",
               "2022-05-19 07:00:00",
               "2022-05-20 07:00:00"
            ]
         },
      ]
   }
]

这是我过滤后的预期结果.请注意,只返回当月有班次的参与者,而allschedules对象只包含至少有一个日期在当月的项目.

推荐答案

似乎您不仅希望过滤participants数组,还希望每个保留的参与者的allschedules数组也被过滤.

因此,最好返回一个新的对象 struct ,以避免输入 struct 发生变化.

此外,当日程表跨越一年以上时,只判断日期的月份部分可能不会给出好的结果.最好判断年份是否匹配:

以下是如何做到这一点:

let participants = [{"participant_id": 2,"name": "Bond James","allschedules": [{"schedule_id": 1,"name": "James_Bond_SIL_January_22 (AM) - 07:00 - 15:00","schedule_type": "Individual Schedule","number_of_shifts": 31,"start_date": "2022-01-01 07:00:00","end_date": "2022-01-31 15:00:00","shifts": ["2022-01-01 07:00:00","2022-01-02 07:00:00","2022-01-03 07:00:00","2022-01-04 07:00:00"]},{"schedule_id": 2,"name": "Bonds-1:2 Schedule","schedule_type": "Individual Schedule","number_of_shifts": 5,"start_date": "2022-01-12 07:00:00","end_date": "2022-01-16 11:00:00","shifts": ["2022-01-12 07:00:00","2022-01-13 07:00:00","2022-01-14 07:00:00","2022-01-15 07:00:00","2022-01-16 07:00:00"]},{"schedule_id": 9,"name": "test april","schedule_type": "Individual Schedule","number_of_shifts": 4,"start_date": "2022-04-09 16:00:00","end_date": "2022-04-12 20:00:00","shifts": ["2022-04-09 16:00:00","2022-04-10 16:00:00","2022-04-11 16:00:00","2022-04-12 16:00:00"]},{"schedule_id": 10,"name": "CP - James Bond","schedule_type": "Individual Schedule","number_of_shifts": 10,"start_date": "2022-04-11 07:00:00","end_date": "2022-05-11 09:00:00","shifts": ["2022-04-11 07:00:00","2022-04-13 07:00:00","2022-04-18 07:00:00","2022-04-20 07:00:00","2022-04-25 07:00:00","2022-04-27 07:00:00","2022-05-02 07:00:00","2022-05-04 07:00:00","2022-05-09 07:00:00","2022-05-11 07:00:00"]},{"schedule_id": 11,"name": "James_Miller_App_Test","schedule_type": "Individual Schedule","number_of_shifts": 30,"start_date": "2022-04-21 07:00:00","end_date": "2022-05-20 15:00:00","shifts": ["2022-04-30 07:00:00","2022-05-01 07:00:00","2022-05-02 07:00:00","2022-05-03 07:00:00","2022-05-04 07:00:00","2022-05-05 07:00:00","2022-05-06 07:00:00","2022-05-07 07:00:00","2022-05-08 07:00:00","2022-05-09 07:00:00","2022-05-10 07:00:00","2022-05-11 07:00:00","2022-05-12 07:00:00","2022-05-13 07:00:00","2022-05-14 07:00:00","2022-05-15 07:00:00","2022-05-16 07:00:00","2022-05-17 07:00:00","2022-05-18 07:00:00","2022-05-19 07:00:00","2022-05-20 07:00:00"]},{"schedule_id": 12,"name": "Grouped Schedule for April 2022","schedule_type": "Shared/Grouped Schedule","number_of_shifts": 3,"start_date": "2022-04-28 12:00:00","end_date": "2022-04-30 18:00:00","shifts": ["2022-04-28 12:00:00","2022-04-29 12:00:00","2022-04-30 12:00:00"]}]},{"participant_id": 3,"name": "Barkley Charles","allschedules": [{"schedule_id": 12,"name": "Grouped Schedule for April 2022","schedule_type": "Shared/Grouped Schedule","number_of_shifts": 3,"start_date": "2022-04-28 12:00:00","end_date": "2022-04-30 18:00:00","shifts": ["2022-04-28 12:00:00","2022-04-29 12:00:00","2022-04-30 12:00:00"]}]}];

const currentMonth = new Date().toLocaleDateString("en-SE").slice(0, 7); // YYYY-MM format
const result = participants.map(participant => ({
    ...participant,
    allschedules: participant.allschedules.filter(({shifts}) => 
        shifts.some(shift => shift.startsWith(currentMonth))
    )
})).filter(({allschedules: {length}}) => length);

console.log(result);

注:我甚至没有在新项目中使用momentjs作为discourage its use名作者.此外,与本机JavaScript进行月度比较非常容易.

Javascript相关问答推荐

如何用显示网格平滑地将元素从一个地方移动到另一个地方?

在Angular中将样式应用于innerHTML

通过嵌套模型对象进行Mongoose搜索

使用JavaScript重新排序行

MathJax可以导入本地HTML文档使用的JS文件吗?

数字时钟在JavaScript中不动态更新

显示图—如何在图例项上添加删除线效果?

如何将react—flanet map添加到remixjs应用程序

Chart.js-显示值应该在其中的引用区域

单个HTML中的多个HTML文件

是什么导致了这种奇怪的水平间距错误(?)当通过JavaScript将列表项元素追加到无序列表时,是否在按钮之间?

让chart.js饼图中的一个切片变厚?

向数组中的对象添加键而不改变原始变量

如何确保预订系统跨不同时区的日期时间处理一致?

将Auth0用户对象存储在nextjs类型脚本的Reaction上下文中

如何在Jest中模拟函数

固定动态、self 调整的优先级队列

Promise.race()返回已解析的promise ,而不是第一个被拒绝的promise

REACT-本机错误:错误类型错误:无法读取未定义的容器的属性

打字脚本中的函数包装键入