【发布时间】:2017-04-05 10:59:34
【问题描述】:
我有两个moment-ranges 数组,代表两个人忙碌的时段。
计算包含他们忙碌的联合时间范围的数组的最佳方法是什么?如果个人的两个忙碌时段重叠,我需要代表他们忙碌的整个时段的时间范围。
这是一个例子:
const firstBusyPeriods = [
{ start: "2017-04-05T10:00:00Z", end: "2017-04-05T12:00:00Z" },
{ start: "2017-04-05T14:00:00Z", end: "2017-04-05T15:00:00Z" }
]
const secondBusyPeriods = [
{ start: "2017-04-05T08:00:00Z", end: "2017-04-05T11:00:00Z" },
{ start: "2017-04-05T16:00:00Z", end: "2017-04-05T17:00:00Z" }
]
生成的数组应如下所示:
const result = [
{ start: "2017-04-05T08:00:00Z", end: "2017-04-05T12:00:00Z" },
{ start: "2017-04-05T14:00:00Z", end: "2017-04-05T15:00:00Z" },
{ start: "2017-04-05T16:00:00Z", end: "2017-04-05T17:00:00Z" }
]
结果包含重叠繁忙时段的并集,然后包括个人之间不重叠的两个时段。
最好将两个数组连接起来,对结果数组进行排序,然后应用 reduce 函数吗?
或者最好是递归地遍历两个数组之一并生成一个堆栈,结果是并集和不相交的句点?
【问题讨论】:
-
答案很好,这是我遇到的类似问题,应该很容易修改:stackoverflow.com/a/32770846/1075247
标签: javascript ecmascript-6 momentjs