我有这个包含约会的数组:
let appointments = [
{"appointmentId": 001239, "subject": "Something", "Client": "Bob", "StartTime": "2020-04-16T11:00:00.000Z", "EndTime": "2020-04-16T11:30:00.000Z", "km": 90},
{"appointmentId": 001240, "subject": "Something", "Client": "Alvera", "StartTime": "2020-04-16T11:00:00.000Z", "EndTime": "2020-04-16T11:30:00.000Z", "km": 50},
{"appointmentId": 001241, "subject": "Something", "Client": "Bob", "StartTime": "2020-04-17T11:00:00.000Z", "EndTime": "2020-04-17T11:30:00.000Z", "km": 30}]
现在我想要的是按客户端计算公里数,例如 Bob:总计:120。Alvera 总计:50。还有 StartTime 和 EndTime 的分钟数差异。通过使用dayjs 库,我计算了时间之间的差异。这是一种对km和Times进行累加的方法。km 完美运行,但是,difference
给出了NaN
. 我究竟做错了什么?
var result = [];
this.appointments.reduce(function (res, value) {
if (!res[value.Client]) {
res[value.Client] = {
km: 0,
Client: value.Client,
StartTime: dayjs(value.StartTime),
EndTime: dayjs(value.EndTime),
difference: dayjs(value.EndTime).diff(dayjs(value.StartTime), 'minute') % 60
};
result.push(res[value.Client])
}
res[value.Client].km += value.km;
res[value.Client].difference += value.difference; //here I try to sum the differences by Client, when I remove this line, I just see the difference of the first appointment.
return res;
}, {});