基本上,如果任何值mailPrice
包含N/A
或“N / A`想返回true其没有发生的样子我缺少的东西,另外,如果有什么更好的方法,而不是写大,如果条件我将不胜感激?
main.js
const drug = {
"isBrand": false,
"drugName": "Hydr",
"drugStrength": "5mg-300mg",
"drugForm": "Tablet",
"mailPrice": {
"costEmployer": "0.0",
"costToday": "N/A",
"deductible": "n/a",
"memberCopayAmount": "0.00",
"penalties": "N/A",
"totalDrugCost": "0.00"
}
}
const priceFilterHandler = (item) => {
let bRet = false;
if (item.mailPrice.costEmployer === "N/A" || item.mailPrice.costEmployer === "n/a" || item.mailPrice.costToday == "N/A" || item.mailPrice.costToday === "n/a" || item.mailPrice.penalties === "N/A" || item.mailPrice.penalties === "n/a" || item.mailPrice.deductible === "N/A" || item.mailPrice.deductible === "n/a") {
bRet = true;
}
return bRet;
};
console.log(priceFilterHandler(drug));
您可以定义一个可能值的数组,并将其重写如下:
const na = ["N/A", "n/a"];
if (na.indexOf(item.mailPrice.costEmployer) != -1
|| na.indexOf(item.mailPrice.costToday) != -1
|| na.indexOf(item.mailPrice.penalties) != -1
|| na.indexOf(item.mailPrice.deductible) != -1) {
...
}
例如,如果您可以将不同的条件应用于na,这将很有用。
const na = ["N/A", "n/a", "na", "NA", "n.a."];