In my array of appointment objects, each object includes the key: "RecurrenceRule"
. This key can either be empty (null
) or contain something like this:
"RecurrenceRule": "FREQ=DAILY;INTERVAL=1;COUNT=5;"
Here is an example of one element in the array:
{
appointmentId: 001239,
subject: "Something",
client: "Bob",
startTime: "2020-04-16T11:00:00.000Z",
endTime: "2020-04-16T11:30:00.000Z",
km: 90,
RecurrenceRule: null,
}
My goal is to iterate through these appointments
using the .reduce() function. If an element has
"RecurrenceRule": "FREQ=DAILY;INTERVAL=1;COUNT=5;"
, I want to extract the value after 'COUNT=' and assign it to a variable called count
. If RecurrenceRule
is null
, then I want to assign 1
to count
. Below is the method I am using:
export class AppointmentComponent implements OnInit {
appointments;
ngOnInit(){
this.getAppointments();
}
getAppointments(){
this.appointmentService.getAppointments().subscribe((data : any) => {
this.appointments= data.appointment;
this.groupByClient();
});
};
groupByClient(){
var result = [];
this.appointments.reduce(function (res, value) {
let diff = dayjs(value.endTime).diff(dayjs(value.startTime), "hour",true) % 60;
if(res[value.RecurrenceRule] !== null) {
let count = parseInt(value.RecurrenceRule.split("COUNT=")[1])
} else {
let count = 1;
}
if (!res[value.client]) {
res[value.client] = {
km: value.km,
client: value.client,
count: this.count,
difference: diff * this.count
};
result.push(res[value.client]);
} else {
res[value.client].km += value.km;
res[value.client].difference += diff;
}
return res;
}, {});
}
}
However, I encounter the error message:
ERROR TypeError: Cannot read property 'count' of undefined
, pointing to the this.count
lines. What could be causing this issue? Could it be related to the nested this.
?
If more information is needed, please do not hesitate to ask.