如何过滤指定时间间隔范围之外的ISO DateTime对象



我有一个ISO格式的startTimeendTime属性对象列表,用于我目前正在研究的一个项目。下面是一个示例:

list = [
{
startTime: '2022-06-26T10:00:00.000Z',
endTime: '2022-06-26T10:30:00.000Z'
},
{
startTime: '2022-06-26T10:35:00.000Z',
endTime: '2022-06-26T11:00:00.000Z'
},
{
startTime: '2022-06-26T11:45:00.000Z',
endTime: '2022-06-26T12:15:00.000Z'
},
{
startTime: '2022-06-26T12:10:00.000Z',
endTime: '2022-06-26T12:25:00.000Z'
},
{
startTime: '2022-06-26T12:20:00.000Z',
endTime: '2022-06-26T12:45:00.000Z'
},
{
startTime: '2022-06-26T12:40:00.000Z',
endTime: '2022-06-26T13:00:00.000Z'
}
]

我有一个对象assignment,它有两个属性,就像上面的list数组中的对象。

const assignment = {
startTime: '2022-06-26T12:00:00.000Z',
endTime: '2022-06-26T12:30:00.000Z'
}

我的任务是从list数组中过滤不属于与assignment的间隔/重叠的对象。例如,我想在我的result数组中包含以下内容,因为它们不重叠/在assignment的间隔内:

const result = [
{
startTime: '2022-06-26T10:00:00.000Z',
endTime: '2022-06-26T10:30:00.000Z'
},
{
startTime: '2022-06-26T10:35:00.000Z',
endTime: '2022-06-26T11:00:00.000Z'
},
{
startTime: '2022-06-26T12:40:00.000Z',
endTime: '2022-06-26T13:00:00.000Z'
}
]

但是我想从我的result数组中排除以下内容,因为它们重叠/在assignment对象的间隔内。

[
{
startTime: '2022-06-26T11:45:00.000Z',
endTime: '2022-06-26T12:15:00.000Z'
},
{
startTime: '2022-06-26T12:10:00.000Z',
endTime: '2022-06-26T12:25:00.000Z'
},
{
startTime: '2022-06-26T12:20:00.000Z',
endTime: '2022-06-26T12:45:00.000Z'
},
]
这是我到目前为止的想法:所以,基本上有5种情况。
Scenario 1: startTime and endTime of a list object are before the startTime of assignment
Scenario 2: startTime of a list object is before the startTime of the assignment but endTime is after startTime and before endTime of the assignment
Scenario 3: startTime and endTime of a list object falls entirely into the interval of the startTime and endTime of the assignment object
Scenario 4: startTime of a list object is after the startTime and before the endTime of assignment but endTime(list object) is after the endTime of the assignment  
Scenario 5: Both startTime and endTime of the list object are after the endTime of the assignment

除了编写大量的if-else条件外,过滤对象列表的最佳方法是什么?我使用Javascript与luxon库。如有任何帮助,我将不胜感激。

您可以通过检查list中的endTimeassignment中的startTime之前,或者list中的startTimeassignment中的endTime之后来检查间隔是否重叠:

list = [
{ startTime: '2022-06-26T10:00:00.000Z', endTime: '2022-06-26T10:30:00.000Z' },
{ startTime: '2022-06-26T10:35:00.000Z', endTime: '2022-06-26T11:00:00.000Z' },
{ startTime: '2022-06-26T11:45:00.000Z', endTime: '2022-06-26T12:15:00.000Z' },
{ startTime: '2022-06-26T12:10:00.000Z', endTime: '2022-06-26T12:25:00.000Z' },
{ startTime: '2022-06-26T12:20:00.000Z', endTime: '2022-06-26T12:45:00.000Z' },
{ startTime: '2022-06-26T12:40:00.000Z', endTime: '2022-06-26T13:00:00.000Z' }
]
const assignment = { startTime: '2022-06-26T12:00:00.000Z', endTime: '2022-06-26T12:30:00.000Z' }
const aDates = { 
startTime: new Date(assignment.startTime),
endTime: new Date(assignment.endTime)
}
const result = list.filter(({ startTime, endTime }) => 
new Date(startTime) > aDates.endTime ||
new Date(endTime) < aDates.startTime
)
console.log(result)
.as-console-wrapper { max-height:100% !important; top: 0 }

注意我已经在循环外将assignment中的日期转换为Date,以节省转换发生多次。

Luxon的工具是Interval,它有overlaps()

const fromISOs = (start, end) => {
const startDt = luxon.DateTime.fromISO(start);
const endDt = luxon.DateTime.fromISO(end);
return luxon.Interval.fromDateTimes(startDt, endDt);
};
const assignment = fromISOs('2022-06-26T12:00:00.000Z', '2022-06-26T12:30:00.000Z');
const ranges = [
['2022-06-26T10:00:00.000Z', '2022-06-26T10:30:00.000Z'],
['2022-06-26T10:35:00.000Z', '2022-06-26T11:00:00.000Z'],
['2022-06-26T11:45:00.000Z', '2022-06-26T12:15:00.000Z'],
['2022-06-26T12:10:00.000Z', '2022-06-26T12:25:00.000Z'],
['2022-06-26T12:20:00.000Z', '2022-06-26T12:45:00.000Z'],
['2022-06-26T12:40:00.000Z', '2022-06-26T13:00:00.000Z']
];
ranges
.map(([start, end]) => fromISOs(start, end)) // create intervals
.filter(i => !i.overlaps(assignment)) // filter out the overlaps
.forEach(i => console.log(i.toISO())) // print the interval ISO (you don't have to do this)
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.4.0/luxon.min.js"></script>

最新更新