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.