'Google Script: Sort Array of Email Addresses to Remove Duplicates
I pull data from a Google Event Calendar. The events include multiple point of contact e-mails (Primary, Secondary, etc.)
I am currently able to collect ALL of the e-mails for the events, and this includes many duplicates.
I would like to take the entire list of emails (currently delimited by ";") and parse through to remove the duplicates.
I created a call function that takes a string (the current list of e-mails) and should return a clean list.
The code I have been trying to use:
function CleanEmail (InputEmailString) {
var ReturnEmail = "";
var EmailArray = [];
var EmailArray = InputEmailString.split(";");
EmailArray.sort().reverse();
var ReturnEmail = EmailArray[0];
for (i=1; i < EmailArray.length; ++i) {
if (EmailArray[i-1] != EmailArray[i]) {
ReturnEmail += ";" + EmailArray[i]
} // End If Statement
} // End For Statement
return ReturnEmail;
} // Close Function
My thought is to sort the Array so that it would be in alphabetical order, then check one email back to see if there is a duplicate.
Regardless of what I try the returned email list does not sort, nor does it remove any duplicates.
Any help is greatly appreciated.
Solution 1:[1]
Description
Using Set
you can create an array of unique values.
Code.gs
function test() {
try {
let InputEmailString = "[email protected];[email protected];[email protected];[email protected];[email protected];[email protected];;[email protected];";
CleanEmail(InputEmailString);
}
catch(err) {
console.log(err);
}
}
function CleanEmail (InputEmailString) {
var ReturnEmail = "";
var EmailArray = [];
var EmailArray = InputEmailString.split(";");
EmailArray = [...new Set(EmailArray)];
EmailArray.sort().reverse();
var ReturnEmail = EmailArray.join(';')
console.log(ReturnEmail);
} // Close Function
Execution Log
11:29:51 AM Notice Execution started
11:29:51 AM Info [email protected];[email protected];[email protected];[email protected];
11:29:51 AM Notice Execution completed
Reference
Solution 2:[2]
If you're just taking a string of emails separated by ;
's > removing duplicates > rejoining them,
Try:
function CleanEmail (InputEmailString) {
return [...new Set(InputEmailString.split(`;`)].join(`;`)
}
Long Version:
function CleanEmail (InputEmailString) {
const emailsArray = InputEmailString.split(`;`);
const uniqueEmailsArray = [...new Set(emailArray)];
const uniqueEmailString = uniqueEmailsArray.join(`;`);
return uniqueEmailString
}
.getGuestList() Method
// const guestList = event.getGuestList()
// const emailList = CleanEmail(guestList)
function CleanEmail (guestListData) {
const emailList = guestListData.map(i => i.getEmail())
return [...new Set(emailList)].join(`;`)
}
Solution 3:[3]
Remove Duplicates
function removedup(eA=["[email protected]","[email protected]"]) {
let uA = [];
eA.forEach(e => {
if (!~uA.indexOf(e)) {
uA.push(e);
}
})
console.log(JSON.stringify(uA));
return uA;
}
Execution log
11:50:44 AM Notice Execution started
11:50:44 AM Info ["[email protected]"]
11:50:45 AM Notice Execution completed
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | TheWizEd |
Solution 2 | |
Solution 3 | Cooper |