我有一个包含约会和花名册的数组。它们由类型分隔。
如何过滤只返回名单中空白的日期范围
除了总的“块”似乎不正确外,它似乎部分起作用。另外,块的最后一部分没有正确的开始和结束日期。
const appointments = [
{
"start": 1549953000000,
"end": 1549985400000,
"id": "roster-item-1",
"type": "background",
"className": "timeline-background__regular"
},
{
"start": 1549959300000,
"end": 1549971000000,
"id": "range-1",
"type": "range",
"className": "timeline-range__regular"
},
{
"start": 1549978200000,
"end": 1549996200000,
"id": "range-1",
"type": "range",
"className": "timeline-range__regular"
},
{
"start": 1550125800000,
"end": 1550158200000,
"id": "roster-item-2",
"type": "background",
"className": "timeline-background__regular"
},
{
"start": 1550471400000,
"end": 1550503800000,
"id": "roster-item-3",
"type": "background",
"className": "timeline-background__regular"
},
{
"start": 1550557800000,
"end": 1550590200000,
"id": "roster-item-4",
"type": "background",
"className": "timeline-background__regular"
},
{
"start": 1550644200000,
"end": 1550676600000,
"id": "roster-item-5",
"type": "background",
"className": "timeline-background__regular"
}
]
const draggedAppointment = {
"type": "range",
"id": "1fe94e2d-59e6-4dbd-a9e7-bcc3f92e7b84",
"start": 1549959300000,
"end": 1550169000000,
};
appointments
.reduce((accumulator, currAppointment, currentIndex, array) => {
const currStart = currAppointment.start;
const currEnd = currAppointment.end;
const prevAppointment = array[currentIndex - 1];
// Only add roster items
if (prevAppointment && prevAppointment.type === 'background') {
const prevStart = prevAppointment.start;
const prevEnd = prevAppointment.end;
// All of previous
if (currStart > prevEnd || currStart < prevStart) {
accumulator.push({
type: currAppointment.type,
id: `${draggedAppointment.id}-${currentIndex}-${currStart}`,
start: prevStart,
end: prevEnd
});
return accumulator;
}
// Start of previous and end of current
if (currStart > prevStart) {
accumulator.push({
type: currAppointment.type,
id: `${draggedAppointment.id}-${currentIndex}-${currEnd}`,
start: prevStart,
end: currStart
});
}
// Beginning of current and end of previous
if (currEnd < prevEnd && currStart < prevEnd) {
accumulator.push({
type: currAppointment.type,
id: `${draggedAppointment.id}-${currStart}-${currentIndex}`,
start: currEnd,
end: prevEnd
});
}
}
// If no previous appointment, push current without id
if (!prevAppointment) {
accumulator.push({
type: currAppointment.type,
start: currStart,
end: currEnd
});
}
return accumulator;
}, [])
.filter(app => app.id)
.map(app => ({
...app,
start: new Date(app.start).toLocaleString(),
end: new Date(app.end).toLocaleString(),
}));
我希望将draggedAppointment划分为“块”,其中名册中有可用空间。