1

i wanted to group by an array of object with multiple field and i have done it and i wanted to concat the email first with respect to the reportName and then with respect to the particular events i have done that part but when i am doing that in the fields i get it undefined with them i want to remove that undefined and print only the strings of email.

var data = [ { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'open' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'open' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'open' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'open' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'open' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'processed' },
  { office: 'abc',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'abc',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'processed' },
  { office: 'def',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'def',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'def',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'def',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'def',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'def',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'def',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'def',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'def',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'open' },
  { office: 'def',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'open' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'open' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'open' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'open' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'ghi',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'ghi',
    reportName: 'payroll',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'hhhfgfg',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'open' },
  { office: 'hhhfgfg',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'hhhfgfg',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'hhhfgfg',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'hhhfgfg',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },
  { office: 'hhhfgfg',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'hhhfgfg',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'delivered' },
  { office: 'hhhfgfg',
    reportName: 'footprints',
    email: '[email protected]',
    event: 'processed' },

 ] 

i have tried to solve this problem using this way

const processArray = data.filter(itms => {
      return itms.event == 'processed';
    });
    const openEvent = data.filter(itms => {
      return itms.event == 'open';
    });
    const recieved = data.filter(itms => {
      return itms.event == 'delivered';
    });
    //console.log(processArray)
    const processEmailRecord = processArray.map(arrayofObj => ({
      processEmail: arrayofObj.email,
      office: arrayofObj.office,
      reportName: arrayofObj.reportName,
    }));

    const openEmailRecord = openEvent.map(arrayofObj => ({
      openEmail: arrayofObj.email,
      office: arrayofObj.office,
      reportName: arrayofObj.reportName,
    }));

    const recEmailRecord = recieved.map(arrayofObj => ({
      recEmail: arrayofObj.email,
      office: arrayofObj.office,
      reportName: arrayofObj.reportName,
    }));


const eventwiseArray = [...processEmailRecord, ...recEmailRecord, ...openEmailRecord];


    let hash1 = Object.create(null);
    let eventArray = [];
    eventwiseArray.forEach(o => {
      var key = ['office', 'reportName']
        .map(function(k) {
          return o[k];
        })
        .join('|');

      if (!hash1[key]) {
        hash1[key] = {office: o.office, reportName: o.reportName,email:' ',processEmail:'',recEmail:'',openEmail:''};
        eventArray.push(hash1[key]);
      }
      ['email'].forEach(k => {
        if (hash1[key] && !hash1[key][k].includes(o[k])) {
          hash1[key][k] += o[k] + ',';
        }
      });
     ['processEmail'].forEach(k => {
        if (hash1[key] && !hash1[key][k].includes(o[k])) {
          hash1[key][k] += o[k] + ',';
        }
      });
      ['recEmail'].forEach(k => {
        if (hash1[key] && !hash1[key][k].includes(o[k])) {
          hash1[key][k] += o[k] + ',';
        }
      });
      ['openEmail'].forEach(k => {
        if (hash1[key] && !hash1[key][k].includes(o[k])) {
          hash1[key][k] += o[k] + ',';
        }
      });


    });

i am getting output in this way

[ { office: 'abc',
    reportName: 'payroll',
    email: ' undefined,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],',
    processEmail: '[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],undefined,',
    recEmail: 'undefined,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],',
    openEmail: 'undefined,[email protected],' },
  { office: 'abc',
    reportName: 'footprints',
    email: ' undefined,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],',
    processEmail: '[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],undefined,',
    recEmail: 'undefined,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],',
    openEmail: 'undefined,[email protected],[email protected],' },
  { office: 'def',
    reportName: 'footprints',
    email: ' undefined,[email protected],[email protected],[email protected],[email protected],',
    processEmail: '[email protected],[email protected],[email protected],[email protected],undefined,',
    recEmail: 'undefined,[email protected],[email protected],[email protected],',
    openEmail: 'undefined,' },
  { office: 'ghi',
    reportName: 'footprints',
    email: ' undefined,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],',
    processEmail: '[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],undefined,',
    recEmail: 'undefined,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],',
    openEmail: 'undefined,[email protected],[email protected],' },
  { office: 'hhhfgfg',
    reportName: 'footprints',
    email: ' undefined,[email protected],[email protected],[email protected],[email protected],[email protected],',
    processEmail: '[email protected],[email protected],[email protected],[email protected],undefined,',
    recEmail: 'undefined,[email protected],[email protected],[email protected],',
    openEmail: 'undefined,[email protected],' },
  { office: 'def',
    reportName: 'payroll',
    email: ' undefined,[email protected],[email protected],',
    processEmail: 'undefined,',
    recEmail: '[email protected],[email protected],undefined,',
    openEmail: 'undefined,[email protected],' },
  { office: 'ghi',
    reportName: 'payroll',
    email: ' undefined,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],',
    processEmail: 'undefined,',
    recEmail: '[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],undefined,',
    openEmail: 'undefined,[email protected],[email protected],' } ]

but i wanted to get the output without undefined written in front of it.all the emails in it must be unique like my solution's output.

2 Answers 2

1

You could group the mails and add the values to the distinct groups by taking a Set for getting unique emails.

var data = [{ office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'open' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'open' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'open' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'open' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'open' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'processed' }, { office: 'abc', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'abc', reportName: 'payroll', email: '[email protected]', event: 'processed' }, { office: 'def', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'def', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'def', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'def', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'def', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'def', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'def', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'def', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'def', reportName: 'payroll', email: '[email protected]', event: 'open' }, { office: 'def', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'open' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'open' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'open' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'open' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'ghi', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'ghi', reportName: 'payroll', email: '[email protected]', event: 'delivered' }, { office: 'hhhfgfg', reportName: 'footprints', email: '[email protected]', event: 'open' }, { office: 'hhhfgfg', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'hhhfgfg', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'hhhfgfg', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'hhhfgfg', reportName: 'footprints', email: '[email protected]', event: 'processed' }, { office: 'hhhfgfg', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'hhhfgfg', reportName: 'footprints', email: '[email protected]', event: 'delivered' }, { office: 'hhhfgfg', reportName: 'footprints', email: '[email protected]', event: 'processed' }],
    result = Object
        .values(data.reduce((r, { office, reportName, event, email }) => {
            var key = [office, reportName].join('|'),
                name = { processed: 'processEmail', delivered: 'recEmail', open: 'openEmail' }[event];

            r[key] = r[key] || { office, reportName, email: new Set };
            r[key].email.add(email);
            if (!r[key][name]) r[key][name] = new Set;
            r[key][name].add(email);
            return r;
        }, {}))
        .map(o => {
            ['email', 'processEmail', 'recEmail', 'openEmail'].forEach(k => {
                if (k in o) o[k] = [...o[k]].join();
            });
            return o;
        });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

in this it work fine but i needed all the email occurs one time not multiple times in the email email id is repeating 2 times and if possible make it possible for other emails also because i don't know how to use includes in it .in my above solution i have used includes.
0

I think you've made this more complicated then it has to be, my approach will be something like this:

var data = [ { office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'open' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'open' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'open' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'open' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'open' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'processed' },{ office: 'abc',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'abc',reportName: 'payroll',email: '[email protected]',event: 'processed' },{ office: 'def',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'def',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'def',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'def',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'def',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'def',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'def',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'def',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'def',reportName: 'payroll',email: '[email protected]',event: 'open' },{ office: 'def',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'open' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'open' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'open' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'open' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'ghi',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'ghi',reportName: 'payroll',email: '[email protected]',event: 'delivered' },{ office: 'hhhfgfg',reportName: 'footprints',email: '[email protected]',event: 'open' },{ office: 'hhhfgfg',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'hhhfgfg',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'hhhfgfg',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'hhhfgfg',reportName: 'footprints',email: '[email protected]',event: 'processed' },{ office: 'hhhfgfg',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'hhhfgfg',reportName: 'footprints',email: '[email protected]',event: 'delivered' },{ office: 'hhhfgfg',reportName: 'footprints',email: '[email protected]',event: 'processed' },];


const result = data.reduce((a, c) => {
  let o = a.find(v => (v.office === c.office && v.reportName === c.reportName));
  if (!o) {
    o = {
      office: c.office,
      reportName: c.reportName,
      email: new Set(),
      recEmail: new Set(),
      processEmail: new Set(),
      openEmail: new Set()
    };
    a.push(o);
  }
  
  o.email.add(c.email);
  
  switch(c.event) {
    case 'processed':
      o.processEmail.add(c.email);
      break;
    case 'delivered':
      o.recEmail.add(c.email);
      break;
    case 'open':
      o.openEmail.add(c.email);
      break;
  }
  return a;
}, [])
.map((v) => ({
  ...v,
  email: [...v.email].join(),
  processEmail: [...v.processEmail].join(),
  recEmail: [...v.recEmail].join(),
  openEmail: [...v.openEmail].join()
}));

console.log(result);

What this does is to first check (the a.find(..) call) if there was already an object created for the office and reportName of the object at the current iteration (c). If it wasn't, one is created and added to the result array and then, the switch statement adds the email to the appropriate property of the found/created object.

This approach is not that efficient because of the .find call at each iteration, it can be improved by using a Map or object which has as keys c.office + c.reportName.

4 Comments

how can i concat all the emails like for email like this ['[email protected],[email protected]'] and same for the other processEmail and others
@amarjeetsingh I've edited my answer to include that.
in this it work fine but i needed all the email occurs one time not multiple times in the email id is repeating 2 times and if possible make it possible for other emails also because i don't know how to use includes in it .in my above solution i have used includes.
@amarjeetsingh I've edited my answer to use Sets instead of Arrays to prevent duplicates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.