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.
CodePudding user response:
It appears that you not only want to filter the participants
array, but also want for each retained participant that its allschedules
array is filtered too.
So, then you would better return a new object structure to avoid that the input structure is mutated.
Also, checking only the month part of a date may not give good results when the schedules span more than one year. It will be better to check that also the year matches:
Here is how it could be done:
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);
NB: I didn't use momentjs as even the authors discourage its use in new projects. Moreover, it is very easy to make the month comparison with native JavaScript.
CodePudding user response:
shifts
actually are the data pieces of start_date
and end_date
, so you just need to check start_date
month is within the current month or not instead of checking all shifts
. If it is, we just need to add it to the result
Moment.js version
//const currentMonth = moment().format("M") //get the actual today
const currentMonth = moment("2022-05-01 12:00:00").format("M")
const result = participants.map((participant) => ({
...participant,
allschedules: participant.allschedules.filter(schedule => moment(schedule.start_date).format("M") === currentMonth || moment(schedule.end_date).format("M") === currentMonth)
})).filter((participant) => participant.allschedules && participant.allschedules.length)
console.log(result)
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"
]
}]
}
]
//simulate today is 1st May 2022
//const currentMonth = moment().format("M") //get the actual today
const currentMonth = moment("2022-05-01 12:00:00").format("M")
const result = participants.map((participant) => ({
...participant,
allschedules: participant.allschedules.filter(schedule => moment(schedule.start_date).format("M") === currentMonth || moment(schedule.end_date).format("M") === currentMonth)
})).filter((participant) => participant.allschedules && participant.allschedules.length)
console.log(result)
.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>
Javascript version (it's more preferable because it's lighter than moment
that would save user traffic to load your scripts)
//simulate today is May 1st 2022
//const currentMonth = new Date().format("M") //get the actual today
const currentMonth = new Date("2022-05-01 12:00:00").getMonth()
const result = participants.map((participant) => ({
...participant,
allschedules: participant.allschedules.filter(schedule => new Date(schedule.start_date).getMonth() === currentMonth || new Date(schedule.end_date).getMonth() === currentMonth)
})).filter((participant) => participant.allschedules && participant.allschedules.length)
console.log(result)
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"
]
}]
}
]
//simulate today is May 1st 2022
//const currentMonth = new Date().format("M") //get the actual today
const currentMonth = new Date("2022-05-01 12:00:00").getMonth()
const result = participants.map((participant) => ({
...participant,
allschedules: participant.allschedules.filter(schedule => new Date(schedule.start_date).getMonth() === currentMonth || new Date(schedule.end_date).getMonth() === currentMonth)
})).filter((participant) => participant.allschedules && participant.allschedules.length)
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }