I need help with transforming an array of objects. So far I have managed to get all the dates between the initial (begin) and the end (end).
This is what I receive and try to transform.
const fetchedOffers = [{
_id: 'dsd87878',
professional: 'd2ycdd',
specialty: '704505',
begin: new Date('2020-04-10T07:00:00.169Z'),
end: new Date('2020-04-11T21:00:00.169Z'),
interval: 1200000,
},
{
_id: 'ddd67878',
professional: '25c0a8',
specialty: '704505',
begin: new Date('2020-06-29T08:00:00.169Z'),
end: new Date('2020-06-30T12:00:00.169Z'),
interval: 1200000,
}
];
const hours = fetchedOffers.reduce((prev, acc) => {
let currentDate = acc.begin.getTime()
const endDate = acc.end.getTime()
while (currentDate < endDate) {
const d = new Date(currentDate)
if (d.getHours() > 7 && d.getHours() < 20 && d.getDay() < 6 && d.getDay() > 0) {
prev.push({
date: new Date(d).toLocaleString('es-CL')
})
}
currentDate = currentDate + acc.interval
}
return prev
}, [])
console.log(hours)
The idea is that you can generate an array of objects like this:
[
{
professional: 'd2ycdd',
specialty: '704505',
date: [
{
offer: 'dsd87878',
date: '4/10/2020',
hours: ['07:00', '07:20', '07:40', '08:00',...]
},
{
offer: 'dsd87878',
date: '4/11/2020',
hours: [..., '20:00', '20:20', '20:40', '21:00']
}
]
},
{
professional: '25c0a8',
specialty: '704505',
date: [
{
offer: 'ddd67878',
date: '6/29/2020',
hours: ['08:00', '08:20', '08:40', '09:00',...]
},
{
offer: 'ddd67878',
date: '6/30/2020',
hours: [..., '11:00', '11:20', '11:40', '12:00']
}
]
},
]
I want to group by professional id and by date (no time). Each date is obtained starting with the beginning date (begin) and adding the interval until reaching the end date. In addition I have added other conditions when displaying the hours, for example omitting hours between 20 and 7 hours and also only from Monday to Friday.
How can I do that? So far I only get all the dates between the start and end date but I need to group by dates and professional
Please login or Register to submit your answer