我正在制作一个可以通过多个条件过滤列表的应用。我正在做的是一个看起来像这样的对象数组:
{
list :[array],
amount: 15000,
type: state,
zipCode: 9xxxx
}
所以我决定使用一个case开关进行一次forEach迭代,该case开关贯穿对象字面量,找出条件的真实性,然后过滤列表。我可以使用下面的代码来运行大多数过滤器,并获得唯一列表。但是由于某种原因,如果我将zipCodeSuppress放入案例中,那么如果将其置于大小写开关的顶部,则将获得0次点击;如果将其置于大小写开关的底部,则不会对其进行注册。
这是代码。任何帮助都很好
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)
})
答案 0 :(得分:1)
switch/case
仅执行匹配的第一种情况。因此,如果zipCodeSuppress == "keepSelect"
为true,则不会检查其他任何条件。
对于每个变量,您应该使用单独的switch/case
。
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;
}