This is different from other similar questions because not one field but campaign, reference and platform should match before adding them
THE PROBLEM
A new report will be uploaded daily, I want to be able to combine all reports.
for example:
{ campaign: "a", reference: "ABC-100", value: 4, date: 27/07/2021, platform: "m, n" }
{ campaign: "a", reference: "ABC-200", value: 6, date: 28/07/2021, platform: "l" }
{ campaign: "b", reference: "DEF-100", value: 2, date: 27/07/2021, platform: "j, k" }
{ campaign: "b", reference: "DEF-100", value: 5, date: 28/07/2021, platform: "j, k" }
and so on....
should become (campaigns with different reference should not be combined)
{ campaign: "a", reference: "ABC-100", value: 4, platform: "m, n" }
{ campaign: "a", reference: "ABC-200", value: 6, platform: "l" }
{ campaign: "b", reference: "DEF-100", value: 7, platform: "j, k" }
I need a function that when passed x number of days should combine last x days reports
for example:
combineReports(2)
should combine last two days reports
WHAT I HAVE TRIED
I have tried combining all campaigns based on their reference
function Filter(reportsData) {
result = [];
reportsData.forEach(function (a) {
if (!this[a.reference]) {
this[a.reference] = {
campaign: a.campaign,
reference: a.reference,
value: 0,
};
result.push(this[a.reference]);
}
this[a.reference].value += a.value;
}, Object.create(null));
console.log("result: ", result);
return result;
}
export default Filter;
See Question&Answers more detail:
os