使用id,billStatus和tDate需要做一个daycounter,计算收到线索的日期。例子 => id: "1" 2 Billable against 8th of May AND 3 Billable for 9th of May than day counter will become => 1+1 = 2。
注意:如果我在一天内收到1个或7个线索,daycount将为1,并在第二天有新的线索时递增。
data: [
{ id: "1", billStatus: "Billable", tDate: "05/08/2020", dayCounter: 0 },
{ id: "1", billStatus: "Billable", tDate: "05/08/2020", dayCounter: 0 },
{ id: "1", billStatus: "Non-Billable", tDate: "05/08/2020", dayCounter: 0 },
{ id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 6 },
{ id: "1", billStatus: "Non-Billable", tDate: "05/09/2020", dayCounter: 0 },
{ id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 0 },
{ id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 0 },
{ id: "3", billStatus: "Billable", tDate: "05/10/2020", dayCounter: 0 },
{ id: "2", billStatus: "Billable", tDate: "05/10/2020", dayCounter: 0 },
{ id: "2", billStatus: "Non-Billable", tDate: "05/10/2020", dayCounter: 0 },
],
retult:
data: [
{ id: "1", dayCounter: 0 },
{ id: "2", dayCounter: 2 },
{ id: "3", dayCounter: 1 }]
你可以检查这个解决方案。虽然我不清楚你的预期输出。但希望它能为你提供一些想法。
const data = [
{ id: "1", billStatus: "Billable", tDate: "05/08/2020", dayCounter: 0 },
{ id: "1", billStatus: "Billable", tDate: "05/08/2020", dayCounter: 0 },
{ id: "1", billStatus: "Non-Billable", tDate: "05/08/2020", dayCounter: 0 },
{ id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 6 },
{ id: "1", billStatus: "Non-Billable", tDate: "05/09/2020", dayCounter: 0 },
{ id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 0 },
{ id: "1", billStatus: "Billable", tDate: "05/09/2020", dayCounter: 0 },
{ id: "3", billStatus: "Billable", tDate: "05/10/2020", dayCounter: 0 },
{ id: "2", billStatus: "Billable", tDate: "05/10/2020", dayCounter: 0 },
{ id: "2", billStatus: "Non-Billable", tDate: "05/10/2020", dayCounter: 0 },
];
const billableDates = data.reduce((res, obj) => {
if (!res[obj.id]) {
if (obj.billStatus === "Billable") {
res[obj.id] = { dates: new Set() };
res[obj.id].dates.add(obj.tDate);
}
} else {
res[obj.id].dates.add(obj.tDate);
}
return res;
}, {});
const modifiedData = data.reduce((store, obj) => {
if (!store[obj.id] && obj.billStatus === "Billable") {
store[obj.id] = obj;
store[obj.id].dayCounter = billableDates[obj.id].dates.size;
store[obj.id].dates = [...billableDates[obj.id].dates];
}
return store;
}, {});
console.log(Object.values(modifiedData));