我有一个ISO格式的startTime
和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'
}
]
我有一个对象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
中的endTime
在assignment
中的startTime
之前,或者list
中的startTime
在assignment
中的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>