I have a list of appointments with a start time, an end time and the practitioner’s id.
const appointments = [ { date: '2022-06-01', start_time: '15:40:00', end_time: '16:10:00', id_professional: 2 }, { date: '2022-06-01', start_time: '16:30:00', end_time: '16:50:00', id_professional: 2 }, { date: '2022-06-01', start_time: '16:30:00', end_time: '16:50:00', id_professional: 3 }, ];
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.
let timeAux = startTimeInSeconds; professionalIds.forEach(professionalId => { const appointmentsAux = appointments.filter(appointment => { return appointment.id_professional === professionalId; }); appointmentsAux.forEach(appointmentAux => { const startTimeAux = appointmentAux.start_time; const [startTimeAuxHours, startTimeAuxMinutes] = startTimeAux.split(':'); const startTimeAuxInSeconds = (parseInt(startTimeAuxHours) * 60 * 60 + parseInt(startTimeAuxMinutes) * 60); const endTimeAux = appointmentAux.end_time; const [endTimeAuxHours, endTimeAuxMinutes] = endTimeAux.split(':'); const endTimeAuxInSeconds = (parseInt(endTimeAuxHours) * 60 * 60 + parseInt(endTimeAuxMinutes) * 60); if(endTimeAuxInSeconds > startTimeInSeconds){ //appointmen.end_time // We need the following appointment to get its start_time and see if it fits the time. //how do we get the next appointment? if (endTimeAuxInSeconds - startTimeAuxInSeconds > startTimeInSeconds){ //insert cita } } }); });
Advertisement
Answer
forEach() has an index parameter you can use
appointmentsAux.forEach((appointmentAux, index) => { const nextAppointment = appointmentsAux[index + 1]; if (nextAppointment) { // do something with the next appointment's info const nextStart = nextAppointment.start_time; } ...