Skip to content Skip to sidebar Skip to footer

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

Solution 1:

//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",
          },
        ];
        
        constcombine = (data) => {
          let combinedData = data.reduce((acc, curr) => {
        
            delete curr.dateif (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)

Solution 2:

Please use this code.

const list = [{ campaign: "a", reference: "ABC-100", value: 4, date: "16/07/2021", platform: "m, n" },
    { campaign: "a", reference: "ABC-200", value: 6, date: "16/07/2021", platform: "l" },
    { campaign: "b", reference: "ABC-100", value: 2, date: "15/07/2021", platform: "j, k" },
    { campaign: "b", reference: "ABC-100", value: 5, date: "14/07/2021", platform: "j, k" }];

functioncombineReports(num) {
    //Calculate dateconst today = newDate(newDate().getFullYear() + '-' + newDate().getMonth() + '-' + (newDate().getDate() + 1));
    const dayFrom = newDate(today.getTime() - num * 3600 * 24 * 1000);
    conststrToDate = (str) => (newDate(str.split('/')[2] + '-' + str.split('/')[1] + '-' + str.split('/')[0]));
    
    //Get compare keysconst keys = Object.keys(list[0]).filter(val => val !== 'value' && val !== 'date');
    
    //Get sub reportsconst partList = list.filter(val =>strToDate(val.date).getTime() < today.getTime() && strToDate(val.date).getTime() >= dayFrom.getTime());
    
    //Combine reportsconstcompare = (report1, report2) => (keys.every(val => report1[val] === report2[val]));
    let result = [];
    partList.forEach(val => {
        if(!result.some(value =>compare(value, val)))
            result.push(val);
        else 
            result = result.map(obj => (compare(obj, val) ? {...obj, value: obj["value"] + val["value"]} : obj));
    });
    console.log(result);
}

combineReports(3);

Post a Comment for "Combine Multiple Items Based On Properties"