I have a list of appointments with a start time, an end time and the practitioner’s id.
JavaScript
x
21
21
1
const appointments = [
2
{
3
date: '2022-06-01',
4
start_time: '15:40:00',
5
end_time: '16:10:00',
6
id_professional: 2
7
},
8
{
9
date: '2022-06-01',
10
start_time: '16:30:00',
11
end_time: '16:50:00',
12
id_professional: 2
13
},
14
{
15
date: '2022-06-01',
16
start_time: '16:30:00',
17
end_time: '16:50:00',
18
id_professional: 3
19
},
20
];
21
I have filtered the appointments that belong to that professional, but now what I want to do is to be able to get the end time of the first appointment and the start time of the next appointment for further processing.
JavaScript
1
25
25
1
let timeAux = startTimeInSeconds;
2
professionalIds.forEach(professionalId => {
3
const appointmentsAux = appointments.filter(appointment => {
4
return appointment.id_professional === professionalId;
5
});
6
appointmentsAux.forEach(appointmentAux => {
7
const startTimeAux = appointmentAux.start_time;
8
const [startTimeAuxHours, startTimeAuxMinutes] = startTimeAux.split(':');
9
const startTimeAuxInSeconds = (parseInt(startTimeAuxHours) * 60 * 60 + parseInt(startTimeAuxMinutes) * 60);
10
11
const endTimeAux = appointmentAux.end_time;
12
const [endTimeAuxHours, endTimeAuxMinutes] = endTimeAux.split(':');
13
const endTimeAuxInSeconds = (parseInt(endTimeAuxHours) * 60 * 60 + parseInt(endTimeAuxMinutes) * 60);
14
15
if(endTimeAuxInSeconds > startTimeInSeconds){ //appointmen.end_time
16
17
// We need the following appointment to get its start_time and see if it fits the time.
18
//how do we get the next appointment?
19
if (endTimeAuxInSeconds - startTimeAuxInSeconds > startTimeInSeconds){
20
//insert cita
21
}
22
}
23
});
24
});
25
Advertisement
Answer
forEach() has an index parameter you can use
JavaScript
1
8
1
appointmentsAux.forEach((appointmentAux, index) => {
2
const nextAppointment = appointmentsAux[index + 1];
3
if (nextAppointment) {
4
// do something with the next appointment's info
5
const nextStart = nextAppointment.start_time;
6
}
7
8