我有一个多维数组的对象,我想过滤如下所述:
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
对象只包含至少有一个日期在当月的项目.