问题
using id, billStatus and tDate need to make a daycounter which counts date of days leads are received. Example => for id:"1" 2 Billable against 8th of may AND 3 Billable for 9th of may than day counter will become => 1+1 = 2
Note: if I get 1 lead or 7 leads for a single day daycount will be 1 and increment the next day when there will be any new lead on a new day.
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 }]
回答1:
You can check this solution. Though i'm not clear about your expected output. But hope it will provide you some idea.
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));
来源:https://stackoverflow.com/questions/61785210/how-to-create-a-date-counter-i-tried-it-using-reduce-function