I am looking for the most efficient way to loop over multiple array of objects and add values to a set.
// This is how filesData object looks
const filesData :
{
hashedEmail: "liugdfkjn83i",
name: "JohnSimith"
}
const setOfHashedEmails = new Set<string>();
for (const filesData of objectDataInS3Folder) {
// filesData is an array of objects
// Loop over the array of objects and add only the hashedItem to a set
filesData.forEach(element=> setOfHashedEmails.add(element.hashedEmail));
}
Is there a better way to loop over the array of objects multiple times and add the hashedEmail to the set (by using spread operator)
Other possible way can be:
const arrayOfAllUsers = [];
for (const filesData of objectDataInS3Folder) {
arrayOfAllUsers.concat(filesData);
}
// Create a set of hashed emails
const setOfHashedEmails = new Set<string>(arrayOfAllUsers.map(item => item.HashedEmail));