I would like to calculate the time period between two appointments.
They are in this format:
const events = [
{
start: eventStart,
end: eventEnd
},
{
start: eventStart,
end: eventEnd
},
// more Events
]
Then I tried mapping over this array, referencing the next element to define the end of the "empty" period.
const empty = events.map((element, index, array) => {
const obj = {
start: element.end,
end: array[index + 1].start
}
return obj
})
this works mostly fine, however the last element will throw an error since it can't reference the next element.
TypeError: Cannot read property 'start' of undefined
How would you solve that problem? Any help is appreciated!
emptyresult to have? If the same number asevents, then what should the value ofendbecome for the last element?