I have a multidimensional array of objects that I want to filter as explained below:
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"
]
}
]
}
]
From my array above, I have an object called: allschedules that has its own objects. The object of interest inside allschedules is: shifts which has an array of dates. The objective is to only return participants that fulfil the condition where the dates inside the shifts array is for the current month.
This is what I've done:
participants.filter((participant) => {
return participant.allschedules.filter((schedule) => {
schedule.shifts.find(
(shift) => moment(shift).format("M") == moment().format("M")
);
});
})
but this returns the exact same object from the defined participants variable. So nothing is filtered. What would be the correct way of approaching this?
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>
My desired filtered array(filtered by current month), would be this:
[
{
"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"
]
},
]
}
]
This is my expected result after filtering. Note that only the participant that has shifts in the current month is returned AND the allschedules object only has items that have atleast one date that is in the current month.
returnon line withfindstatement.