Mickey Gray
Mickey Gray

Reputation: 460

Case Switch Filter in For Each

I am making an app that filters lists by multiple criteria. What I am doing it with is an object array that looks something like:

{
list :[array],
amount: 15000,
type: state,
zipCode: 9xxxx
}

so I have decided to do a forEach iteration with a case switch that runs through the object literal and finds truthiness of conditions and then filters list. I am able to run most of my filters and get a unique list using the following bed of code. But for some reason if I put in the case for zipCodeSuppress, I get 0 hits if its at the top of the case switch and it doesn't register it if its at the bottom of the case switch.

Here is the code. Any help would be great

 drop.forEach( async (tollFree) => {
      //console.log(Object.entries(tollFree));
      const zipCodes = Object.values(tollFree)[0].split(",");
     //   console.log(tollFree)

      let updatedList = []
      const {
        lienType,
        tracking,
        mailList,
        lienAmount,
        vendor,
        postageCeiling,
        unitCost,
        mailHouse,
        date,
        title, 
        zipCodeSuppress,
      } = tollFree;


   console.log(zipCodeSuppress)

   zips = await Zip.find({
    "class": { "$in": zipCodes },
  });
   
  zips = zips.map(zip => zip.zip4)

   switch (true) {
        case zipCodeSuppress == "keepSelect":
          updatedList.push(mailList.filter((e) => zips.includes(e.zip4.substring(0,4))))
          break;
        case lienAmount == "15000":
          updatedList.push(mailList.filter((e) => e.amount <= 15000));
          break;
        case lienAmount == "25000":
        updatedList.push(mailList.filter(
            (e) => e.amount >= 15000 && e.amount <= 25000
          ));
          break;
        case lienAmount == "50000":
        updatedList.push(mailList.filter((e) => e.amount >= 25000 && e.amount <= 50000));
          break;
        case lienAmount == "100000":
        updatedList.push(mailList.filter(
            (e) => e.amount >= 50000 && e.amount <= 100000
          ));
          break;
        case lienAmount == "10000000":
          updatedList.push(mailList.filter((e) => e.amount > 100000));
          break;
        case vendor == "ftls":
          updatedList.push(mailList.filter((e) => e.pinCode.length === 7));
          break;
        case vendor == "risk":
          updatedList.push(mailList.filter((e) => e.pinCode.length === 10));
          break;
        case vendor == "advance":
          updatedList.push(mailList.filter((e) => e.pinCode.length === 12));
          break;
        case vendor == "atype":
          updatedList.push(mailList.filter((e) => e.pinCode.length === 15));
          break;
        case lienType == "state":
         updatedList.push(mailList.filter((e) => e.fileType == "State Tax Lien"));
          break;
        case lienType == "federal":
         updatedList.push(mailList.filter((e) => e.fileType == "Federal Tax Lien"));
          break;
        default:
          return mailList;
      }
      updatedList = updatedList.flat()

      console.log(updatedList.length)
    })


Upvotes: 0

Views: 352

Answers (1)

Barmar
Barmar

Reputation: 781741

switch/case only executes the first case that matches. So if zipCodeSuppress == "keepSelect" is true, it won't check for any of the other conditions.

You should use a separate switch/case for each variable.

switch (zipCodeSuppress) {
  case "keepSelect":
    updatedList.push(mailList.filter((e) => zips.includes(e.zip4.substring(0, 4))))
    break;
}
switch (lienAmount) {
  case "15000":
    updatedList.push(mailList.filter((e) => e.amount <= 15000));
    break;
  case "25000":
    updatedList.push(mailList.filter(
      (e) => e.amount >= 15000 && e.amount <= 25000
    ));
    break;
  case "50000":
    updatedList.push(mailList.filter((e) => e.amount >= 25000 && e.amount <= 50000));
    break;
  case "100000":
    updatedList.push(mailList.filter(
      (e) => e.amount >= 50000 && e.amount <= 100000
    ));
    break;
  case "10000000":
    updatedList.push(mailList.filter((e) => e.amount > 100000));
    break;
}
switch (vendor) {
  case "ftls":
    updatedList.push(mailList.filter((e) => e.pinCode.length === 7));
    break;
  case "risk":
    updatedList.push(mailList.filter((e) => e.pinCode.length === 10));
    break;
  case "advance":
    updatedList.push(mailList.filter((e) => e.pinCode.length === 12));
    break;
  case "atype":
    updatedList.push(mailList.filter((e) => e.pinCode.length === 15));
    break;
}
switch (lienType) {
  case "state":
    updatedList.push(mailList.filter((e) => e.fileType == "State Tax Lien"));
    break;
  case "federal":
    updatedList.push(mailList.filter((e) => e.fileType == "Federal Tax Lien"));
    break;
}

Upvotes: 1

Related Questions