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: 'amar045@gmail.com',
office: 'xyz.in',
reportName: 'payroll',
},
{
email: 'amar0455@gmail.com',
office: 'xyz.in',
reportName: 'payroll',
},
{
email: 'amar0455@gmail.com',
office: 'xyz.in',
reportName: 'payroll',
},
{
email: 'amar045@gmail.com',
office: 'xyz.in',
reportName: 'payroll',
},
{
email: 'amar0445@gmail.com',
office: 'yyy.in',
reportName: 'delievered',
},
{
email: 'amar0445@gmail.com',
office: 'yyy.in',
reportName: 'delievered',
},
{
email: 'amar0425@gmail.com',
office: 'yyy.in',
reportName: 'payroll',
},
{
email: 'amar0425@gmail.com',
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: 'amar045@gmail.com, amar0455@gmail.com, amar0425@gmail.com'
},
{
office: 'yyy.in',
reportName: 'delievered',
email: 'amar0445@gmail.com, amar0425@gmail.com'
},
{
office: 'yyy.in',
reportName: 'payroll',
email: 'amar0425@gmail.com'
}
]