gpt4 book ai didi

javascript - 如何根据时隙将对象时间序列数组拆分为多个时间序列?

转载 作者:行者123 更新时间:2023-12-05 08:46:51 24 4
gpt4 key购买 nike

我想根据每小时的时间段拆分我的开始时间和结束时间数据。

input = [
//Type A, it lies between 7 to 8 time slot
{ "start_time": "2021-08-20T07:00:00.000", "end_time": "2021-08-20T07:25:00.000", "Type": "A" },
//Type B , Time lies between 8, 9 time slot, so need to split into two new objects
{ "start_time": "2021-08-20T07:25:00.000", "end_time": "2021-08-20T08:39:49.000", "Type": "B" },
//Type C, Time lies between three slots 8, 9, 10, and 11. SO need to split in 4 objects.
{ "start_time": "2021-08-20T08:39:50.000", "end_time": "2021-08-20T11:02:34.000", "Type": "C" }
]

预期输出:

var output = [
{ "start_time": "2021-08-20T07:00:00.000", "end_time": "2021-08-20T07:25:00.000", "Type": "A" },
{ "start_time": "2021-08-20T07:25:00.000", "end_time": "2021-08-20T08:00:00.000", "Type": "B" },
{ "start_time": "2021-08-20T08:00:00.000", "end_time": "2021-08-20T08:39:49.000", "Type": "B" },
{ "start_time": "2021-08-20T08:39:49.000", "end_time": "2021-08-20T09:00:00.000", "Type": "C" },
{ "start_time": "2021-08-20T09:00:00.000", "end_time": "2021-08-20T10:00:00.000", "Type": "C" },
{ "start_time": "2021-08-20T10:00:00.000", "end_time": "2021-08-20T11:00:00.000", "Type": "C" },
{ "start_time": "2021-08-20T11:00:00.000", "end_time": "2021-08-20T11:02:34.000", "Type": "C" }
]

我使用 moment-range 来创建时间槽。

const day_start=moment().startOf('day').hours(7);
const day_end=moment().startOf('day').hours(15)
const day=moment.range(day_start,day_end)
const time_slots = Array.from(day.by('minutes',{step:60}))

我得到的输出:

    const hourly_data = [moment("2021-08-20T07:00:00.000"),
moment("2021-08-20T08:00:00.000"),
moment("2021-08-20T09:00:00.000"),

...
moment("2021-08-20T13:00:00.000"),
moment("2021-08-20T14:00:00.000"),
moment("2021-08-20T1500:00.000")]

我按要求获得了每小时范围,但无法拆分。

对获得上述输出有什么帮助吗?

最佳答案

如果你想在你的问题中写入输出,你可以试试这个。但是您可以在不使用 range 的情况下做到这一点。

在每种情况下,您都必须检查时隙的结束时间是在范围限制之前还是之后,并使用较低的时间

window['moment-range'].extendMoment(moment); // just declaration

const input = [
//Type A, it lies between 7 to 8 time slot
{ "start_time": "2021-08-20T07:00:00.000", "end_time": "2021-08-20T07:25:00.000", "Type": "A" },
//Type B , Time lies between 8, 9 time slot, so need to split into two new objects
{ "start_time": "2021-08-20T07:25:00.000", "end_time": "2021-08-20T08:39:49.000", "Type": "B" },
//Type C, Time lies between three slots 8, 9, 10, and 11. SO need to split in 4 objects.
{ "start_time": "2021-08-20T08:39:50.000", "end_time": "2021-08-20T11:02:34.000", "Type": "C" }
]
const output = [];

for (const rangeDetails of input) {
const { start_time, end_time, Type } = rangeDetails;
const dateTimeStart = moment(start_time);
const dateTimeEnd = moment(end_time);

const range = moment.range(dateTimeStart, dateTimeEnd);

for (const startDateSlot of range.by('minutes', { step: 60 })) {
const help = { Type };
help.start_time = startDateSlot.clone();
help.end_time = moment(startDateSlot).add(60, 'minutes').isBefore(dateTimeEnd) ? moment(startDateSlot).add(60, 'minutes') : dateTimeEnd.clone();

output.push(help);
}
}
console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdn.tutorialjinni.com/moment-range/4.0.2/moment-range.js"></script>

没有 range 的替代方案是:

const input = [
//Type A, it lies between 7 to 8 time slot
{ 'start_time': '2021-08-20T07:00:00.000', 'end_time': '2021-08-20T07:25:00.000', 'Type': 'A' },
//Type B , Time lies between 8, 9 time slot, so need to split into two new objects
{ 'start_time': '2021-08-20T07:25:00.000', 'end_time': '2021-08-20T08:39:49.000', 'Type': 'B' },
//Type C, Time lies between three slots 8, 9, 10, and 11. SO need to split in 4 objects.
{ 'start_time': '2021-08-20T08:39:50.000', 'end_time': '2021-08-20T11:02:34.000', 'Type': 'C' }
];

const output = [];

for (const rangeDetails of input) {
const { start_time, end_time, Type } = rangeDetails;

const dateTimeStart = moment(start_time);
const dateTimeEnd = moment(end_time);

while (dateTimeStart.isBefore(dateTimeEnd)) {
const help = { Type, start_time: dateTimeStart.clone() }
dateTimeStart.add(60, 'minutes'); // This edit dateTimeStart, adding 60 minutes

if (dateTimeStart.isAfter(dateTimeEnd)) help.end_time = dateTimeEnd.clone();
else help.end_time = dateTimeStart;

output.push(help);
}
}
console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

关于javascript - 如何根据时隙将对象时间序列数组拆分为多个时间序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68917894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com