Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

javascript - Combine multiple items based on properties

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
//One way is to try reduce    

let arr = [
          {
            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",
          },
        ];
        
        const combine = (data) => {
          let combinedData = data.reduce((acc, curr) => {
        
            delete curr.date
        
            if (acc.length == 0) {       
              acc.push(curr);
              return acc;
            }
        
            let d = acc.find(
              (x) =>
                x.campaign == curr.campaign &&
                x.reference == curr.reference &&
                x.platform == curr.platform
            );
        
            if(d){
                d.value += curr.value
            }
            else{
                acc.push(curr)
            }
        
            return acc
        
        
          }, []);
        
          return combinedData
        };
        
        
        let result = combine(arr)
        
        console.log(result)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...