0

I want to group an array of object which i have done using two values office and reportName but i want email id in a string which I am getting but the email id is not unique.i have to try to add all the email but when I try concat it gives an empty string. can anyone knows how to do this

var data = [{
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'yyy.in',
    reportName: 'delievered',
  },
  {
    email: '[email protected]',
    office: 'yyy.in',
    reportName: 'delievered',
  },
  {
    email: '[email protected]',
    office: 'yyy.in',
    reportName: 'payroll',
  },

  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'delievered',
  }
];

let hash = Object.create(null);
let grouped = [];

data.forEach(function(o) {
  var key = ['office', 'reportName']
    .map(function(k) {
      return o[k];
    })
    .join('|');

  if (!hash[key]) {
    hash[key] = {
      office: o.office,
      reportName: o.reportName,
      email: ''
    };
    grouped.push(hash[key]);
  }
  ['email'].forEach(function(k) {
    hash[key][k] += o[k].split(',');
  });
});
console.log(grouped);

expected output is

[
  {
    office: 'xyz.in',
    reportName: 'payroll',
     email: '[email protected], [email protected], [email protected]'
  },
  {
    office: 'yyy.in',
    reportName: 'delievered',
    email: '[email protected], [email protected]'
  },
  {
    office: 'yyy.in',
    reportName: 'payroll',
    email: '[email protected]'
  }
]
1

3 Answers 3

1

You can use "includes" method of String to check if the email exists already and update the email attribute of the object.

var data = [
  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'yyy.in',
    reportName: 'delievered',
  },
  {
    email: '[email protected]',
    office: 'yyy.in',
    reportName: 'delievered',
  },
  {
    email: '[email protected]',
    office: 'yyy.in',
    reportName: 'payroll',
  },

  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'delievered',
  }
];

let hash = Object.create(null);
let grouped = [];

data.forEach(function(o) {
  var key = ['office', 'reportName']
    .map(function(k) {
      return o[k];
    })
    .join('|');

  if (!hash[key]) {
    hash[key] = {office: o.office, reportName: o.reportName, email: ''};
    grouped.push(hash[key]);
  }
  ['email'].forEach(function(k) {
    if (hash[key] && !hash[key][k].includes(o[k])){
        hash[key][k] += o[k] + ',';
      }
    });
});
console.log(grouped);

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

Comments

1

If you want to group by office and reportName, then you can reduce method and some method to prevent inserting of duplicates:

const result = [...data.reduce((r, o) => {
    const key = o.office + '-' + o.reportName;
    const item = r.get(key) || Object.assign({}, o, {
        email: []
    });

    if (!item.email.some(s=> s == o.email))
        item.email.push(o.email);

    return r.set(key, item);
}, new Map).values()];

An example:

var data = [
  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'payroll',
  },
  {
    email: '[email protected]',
    office: 'yyy.in',
    reportName: 'delievered',
  },
  {
    email: '[email protected]',
    office: 'yyy.in',
    reportName: 'delievered',
  },
  {
    email: '[email protected]',
    office: 'yyy.in',
    reportName: 'payroll',
  },

  {
    email: '[email protected]',
    office: 'xyz.in',
    reportName: 'delievered',
  }
];


const result = [...data.reduce((r, o) => {
    const key = o.office + '-' + o.reportName;
    const item = r.get(key) || Object.assign({}, o, {
        email: []
    });

    if (!item.email.some(s=> s == o.email))
        item.email.push(o.email);

    return r.set(key, item);
}, new Map).values()];

console.log(Object.values(result));

Comments

0

You can use reduce to group your data according to office and reportName

I've used the Set object as a convenient way to ensure we don't duplicate email addresses.

For example:

const data = [ { email: '[email protected]', office: 'xyz.in', reportName: 'payroll', }, { email: '[email protected]', office: 'xyz.in', reportName: 'payroll', }, { email: '[email protected]', office: 'xyz.in', reportName: 'payroll', }, { email: '[email protected]', office: 'xyz.in', reportName: 'payroll', }, { email: '[email protected]', office: 'yyy.in', reportName: 'delievered', }, { email: '[email protected]', office: 'yyy.in', reportName: 'delievered', }, { email: '[email protected]', office: 'yyy.in', reportName: 'payroll', }, { email: '[email protected]', office: 'xyz.in', reportName: 'delievered', } ]; 

const result = Object.values(data.reduce((acc, { office, reportName, email} ) => { 
    const key = office + "_" + reportName;
    if (!acc[key]) acc[key] = { office, reportName };
    acc[key].email = [...new Set([...(acc[key].email || []), email])];
    return acc;
}, {}));

console.log("Data grouped by office and reportName:");
console.log(result);

Comments

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.