Home > Net >  Filter Array Of Objects by Imported CSV
Filter Array Of Objects by Imported CSV

Time:12-07

I currently have an array of objects that look like this but with a ton more entries,

[
  {
    owningrep: "Some name",
    id: 1,
    emails: "[email protected];[email protected]"
  },
  {
    owningrep: "Other name",
    id: 2,
    emails: "[email protected]"
  }
]

I also provide the option to import a CSV of emails, which I then take all of the values and put them in an array.

My question is, given this array of objects, and this array of CSV values, how would I then be able to filter the array of objects to NOT include objects where any of the emails in the csv appear in the email value? Keep in mind some objects might have one email, others might have multiple separated by a semicolon.

Thanks in advance

I've attempted to simply filter the array with includes, but that only seems to cut off a few entries.

let inc = inclusionsList.value.length > 0 && inclusionsList.value['0'] != 0 formatDataAsArray(data).filter(d => _.includes(inclusionsList.value, d.id)) : data;

let fromCSV = formatDataAsArray(inc).filter(i => !_.includes(exclusionCSV.value, i.Emails));

Ultimately what I want to do is take an array of objects like:

[
  {
    owningrep: "Some name",
    id: 1,
    emails: "[email protected];[email protected]"
  },
  {
    owningrep: "Other name",
    id: 2,
    emails: "[email protected]"
  }
]

And an array of emails like:

["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"]

And filter the array of objects so that the only objects left are ones whose email key does not include any of the emails in the array.

CodePudding user response:

If I understrand well your problem and your data here is my proposition:

const exclusionCSV = {value: ['mail1', '[email protected]']}
const inclusionList = {value: [
  {
    owningrep: "Some name",
    id: 1,
    emails: "[email protected];[email protected]"
  },
  {
    owningrep: "Other name",
    id: 2,
    emails: "[email protected]"
  }
]}

const setExclusionMail = new Set(exclusionCSV.value)

const result = inclusionList.value.filter(({emails}) => {
  const mails = emails.split(';')
  return !mails.some(m => setExclusionMail.has(m))
})
  • Related