Search code examples
javascriptnode.jsarrayslodashmailchimp

Problem with _.filter method in lodash in node js


My objective is to delete every row in a CSV for which there are already email addresses in mailchimp.

I tried this function but it doesn't give me the right number of elements:

async function testDeleteEmails(listID, csvFileLocation){
  csvContent = fs.readFileSync(csvFileLocation, "utf-8");
  let { data } = Papa.parse(csvContent, { header: true, skipEmptyLines: true });
  let duplicateEmails = await compareArrayEmailsMailchimpAndCSV (fetchMailchimpMembersEmails(listID), getArrayEmailsFromLocalCSV(csvFileLocation))
  let newCSV = _.filter(data, function(row){
      return _.includes(duplicateEmails, row["Email of user"])
  })
  console.log(newCSV)
}
testDeleteEmails("MY LIST ID", ".../myCSV.csv")

compareArrayEmailsMailchimpAndCSV is a function to get an array of all duplicate email addresses (those in mailchimp and those in my CSV)

I have 1058 email addresses in mailchimp. 960 email addresses in my CSV and my function compareArrayEmailsMailchimpAndCSV give me 797 email addresses so if I'm correct I have 163 email addresses to keep in my CSV. When I'm using the function testDeleteEmails it give me 869 email addresses, which is not what I want. Am I using the lodash method wrongly?

Knowing that I have to do it dynamically because I have to do it with other list and CSV file.

I know the function is not finished, when the newCSV data will be correct, I'll just have to unparse the data and put it in a CSV file.


Solution

  • I think the issue doesn't regarding the Lodash filter. I think the issue is in your CSV file. Maybe there are duplicate email addresses in the CSV. Please check Your CSV file. You can check it using the uniqueBy function from the Lodash.

      const uniqueData = _.uniqBy(data, 'Email of user');
    

    And check the lengths of the data and uniqueData.