我将尽我所能解释这一点。
我有一系列DD / MM / YYYY形式的独特日期,如下所示:
const sortDates = [
'14/04/2018',
'12/04/2018',
'10/04/2018',
]
然后我有一个包含以下信息的对象数组。
const bookings = [
{ bookingStart: '2018-04-14 00:30:00' },
{ bookingStart: '2018-04-14 00:42:00' },
{ bookingStart: '2018-04-14 13:35:00' },
{ bookingStart: '2018-04-12 12:30:00' },
];
我想要实现的目标是每天每小时查找和分组数据。示例如下:
const groupedData = [{
date: '2018-04-14',
period : {
startTime: '00:00',
endTime: '00:59',
total: 2, //total bookings found 2018/04/14 00:00 -> 00:59
},
date: '2018-04-14',
period : {
startTime: '01:00',
endTime: '01:59',
total: 0, //total bookings found 2018/04/14 01:00 -> 01:59
},
...etc
}
然而,我有点卡住了。我当前的代码如下,并且不确定return语句应该如何:
const groupData = sortDates.map((date) => {
for (let i = 0; i <= 23; i += 1) {
const startTime = new Date(date);
const endTime = new Date(date);
startTime.setHours(i);
startTime.setMinutes(0);
startTime.setSeconds(0);
startTime.setMilliseconds(0);
endTime.setHours(i);
endTime.setMinutes(59);
endTime.setSeconds(0);
endTime.setMilliseconds(0);
bookings.map((booking) => {
const dateTime = new Date(booking.bookingStart);
if (startTime.getTime() >= dateTime.getTime()
&& dateTime.getTime() <= endTime.getTime()) {
return {
date: dateTime,
};
}
return null;
});
}
return null;
});
甚至不确定是否是最好或最快的方式,但我如何以我想要的格式返回?更快的替代方案?
答案 0 :(得分:2)
我会避免完全弄乱Dates,而只是比较字符串:
const hmsToHM = hms => hms.match(/^\d\d:\d\d/)[0];
const bookings = [
{ bookingStart: '2018-04-14 00:30:00' },
{ bookingStart: '2018-04-14 00:42:00' },
{ bookingStart: '2018-04-14 00:42:22' },
{ bookingStart: '2018-04-14 00:42:44' },
{ bookingStart: '2018-04-14 13:35:00' },
{ bookingStart: '2018-04-14 00:30:55' },
{ bookingStart: '2018-04-12 12:30:00' },
];
const groupedData = bookings.reduce((groupedData, { bookingStart }) => {
const [date, time] = bookingStart.split(' ');
const startHourMin = hmsToHM(time);
const groupedObj = (() => {
const foundObj = groupedData
.find(({ date: findGroupDate, period: {startTime }}) => {
const findStartHourMin = hmsToHM(startTime);
return findGroupDate === date && findStartHourMin === startHourMin;
});
if (foundObj) return foundObj;
const newObj = { date, period: {
startTime: startHourMin + ':00',
endTime: startHourMin + ':59',
total: 0,
}}
groupedData.push(newObj);
return newObj;
})();
groupedObj.period.total++;
return groupedData;
}, []);
console.log(groupedData);
答案 1 :(得分:1)
使用moment.js
的解决方案:
sortDates.forEach(date => {
const dateMoment = moment(date, 'DD/MM/YYYY');
const bookingsOnDate = bookings.filter(b =>
dateMoment.isSame(b.bookingStart, 'day'),
);
const start = moment(dateMoment);
const end = moment(dateMoment);
for (let i = 0; i < 24; i++) {
start.hour(i);
start.minutes(0);
end.hour(i);
end.minutes(59);
const foundCount = bookingsOnDate.filter(b => {
const bookingMoment = moment(b.bookingStart);
return (
bookingMoment.isSameOrAfter(start) && bookingMoment.isSameOrBefore(end)
);
}).length;
if (foundCount > 0) {
groupedData.push({
date: dateMoment.format('YYYY-MM-DD'),
period: {
startTime: start.format('HH:mm:ss'),
endTime: end.format('HH:mm:ss'),
total: foundCount,
},
});
}
}
});