Reputation: 546
I have a javascript array like so:
var recipients = [{
name: 'Michael',
task: 'programming',
contactdetails: '[email protected]'
}, {
name: 'Michael',
task: 'designing',
contactdetails: '[email protected]'
}, {
name: 'Shane',
task: 'designing',
contactdetails: '[email protected]'
}];
What I am doing is a rostering system where I send out notifications for who is on for this week, so the email is like "Hi Michael you are programming this week". At the moment it is not great because it sends out an email for every value in the array. So in the above instance it would send Michael 2 emails.
What I would like to do is remove duplicates while merging the task property strings. So the array would be:
var recipients = [{
name: 'Michael',
task: 'programming, designing',
contactdetails: '[email protected]'
}, {
name: 'Shane',
task: 'designing',
contactdetails: '[email protected]'
}];
that way it can just send one message like "Hi Michael you are programming, designing this week". How do I go about this? I also am using Google Apps script so I need a pure javascript solution. I should also add that the name and email address for each person will always be identical, so Michael will never have a different email address etc. Your help is much appreciated!
Upvotes: 1
Views: 419
Reputation: 3019
Convert array into an object with key as name
(can be email also)
// original array
var recipients = [
{name: 'Michael',task:'programming',contactdetails:'[email protected]'},
{name: 'Michael',task:'designing',contactdetails:'[email protected]'},
{name: 'Shane',task:'designing',contactdetails:'[email protected]'}
];
var recipientsObj = {};
for (var i = 0; i < recipients.length; i++) {
var element = recipients[i];
var recipientInObj = recipientsObj[element.name]
if (recipientInObj) {
// If a recipient is repeated with same task, here duplicates will appear
recipientInObj.task += ', ' + element.task;
} else {
recipientsObj[element.name] = element;
}
}
console.log(recipientsObj)
Upvotes: 1
Reputation: 4876
Iterate and look for same object if then append tasks like this
var recipients = [{
name: 'Michael',
task: 'programming',
contactdetails: '[email protected]'
}, {
name: 'Michael',
task: 'designing',
contactdetails: '[email protected]'
}, {
name: 'Shane',
task: 'designing',
contactdetails: '[email protected]'
}];
var uniqueR = [];
var copyRecipients = JSON.parse(JSON.stringify(recipients));
copyRecipients .forEach(function(ele){
var obj = uniqueR.find(function(e){
return (e.name == ele.name && e.contactdetails == ele.contactdetails);
});
if(obj){
obj.task = obj.task + ", " + ele.task;
}else{
uniqueR.push(ele);
}
});
console.log(uniqueR)
Upvotes: 1
Reputation: 1
var newJSON = {}; $.each(recipients, function(i, json){ newJSON[json.contactdetails] = {name : json["name"], task : newJSON[json.contactdetails]!= undefined && newJSON[json.contactdetails]["task"]!= undefined ? newJSON[json.contactdetails]["task"] + ", " + json["task"] : json["task"] }
});
Upvotes: 0
Reputation: 2528
var recipients = [{name: 'Michael',task:'programming',contactdetails:'[email protected]'},{name: 'Michael',task:'designing',contactdetails:'[email protected]'},{name: 'Shane',task:'designing',contactdetails:'[email protected]'}];
var tempObj = {};
for (i=0; i<recipients.length; i++) {
if (!tempObj[recipients[i]['name']]) {
tempObj[recipients[i]['name']] = {};
tempObj[recipients[i]['name']]['task'] = [];
}
tempObj[recipients[i]['name']]['task'].push(recipients[i]['task']);
tempObj[recipients[i]['name']]['contactdetails'] = recipients[i]['contactdetails'];
}
var new_arr = [];
Object.keys(tempObj).forEach(function(key) {
new_arr.push({name: key, task: tempObj[key]['task'].join(", "), contactdetails: tempObj[key]['contactdetails']})
});
Upvotes: 1
Reputation: 6442
This would be a good opportunity to use the reduce
function.
What we do is cycle through each of the original recipients list, see if we have already processed the element, if we have, append the task of the current element to the already processed element, otherwise, add the current recipient to the processed list
// original array
var recipients = [
{name: 'Michael',task:'programming',contactdetails:'[email protected]'},
{name: 'Michael',task:'designing',contactdetails:'[email protected]'},
{name: 'Shane',task:'designing',contactdetails:'[email protected]'}
];
var recipientKeyList = []; // used to store the contacts we've already processed
// cycle through each recipient element
var newRecipients = recipients.reduce(function(allRecipients, recipient){
// get the indexOf our processed array for the current recipient
var index = recipientKeyList.indexOf(recipient.contactdetails);
// if the contact details already exist, append the task
if( index >= 0){
allRecipients[index].task = allRecipients[index].task + ', ' + recipient.task;
return allRecipients
}else{ // otherwise append the recipient
recipientKeyList.push(recipient.contactdetails)
return allRecipients.concat(recipient);
}
}, []);
Upvotes: 3