I have a set of objects, each containing four properties (id, start, end, and amount).
Using this data, I generate a new set of objects with the following attributes: date, duration, and amount. The date corresponds to the timestamp of the start property, while the duration represents the time difference in minutes between the start and end timestamps.
Next, I define two variables representing the selected month and year. I then filter the date array based on these chosen month and year values. For instance, if month 11 is chosen, only objects with dates from November would be included, and the same applies to the year. This results in a list of days corresponding to the selected month and year.
const arrayOfDates = [
{
id: 1,
start: '2021-12-01T12:10:56+0000',
end: '2021-12-01T12:00:56+0000',
amount: 10
},
{
id: 2,
start: '2021-12-03T11:10:56+0000',
end: '2021-12-03T12:45:56+0000',
amount: 4
},
{
id: 3,
start: '2021-12-07T09:10:56+0000',
end: '2021-12-07T09:25:56+0000',
amount: 8
},
{
id: 4,
start: '2021-11-24T11:10:56+0000',
end: '2021-11-24T11:25:56+0000',
amount: 8
}
]
const selectedMonth = 12
const selectedYear = 2021
const mapped = arrayOfDates.map((el) => ({
date: new Date(el.start).toLocaleDateString('en-CA'),
duration: new Date(new Date(el.end).getTime() - new Date(el.start).getTime()).getMinutes(),
amount: el.amount
}))
const filteredBySelectedDate = mapped.filter((el) => {
var [year, month] = el.date.split('-')
return selectedMonth === +month && selectedYear === +year;
})
const mappedByDays = filteredBySelectedDate
In addition to displaying elements by day as currently done, I intend to enhance the filteredBySelectedDate
array by grouping entries into weeks within the month. This means creating an array structure like:
const mappedByWeeks = [
{
interval: '12/01 - 12/04',
duration: 85,
amount: 14
},
{
interval: '12/05 - 12/11',
duration: 15,
amount: 8
}
]