温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - How to check same string value for multiple properties and shorthand if statement?

javascript - 如何检查多个属性和速记if语句的相同字符串值?

发布于 2020-03-27 10:41:57

基本上,如果任何值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));

查看更多

查看更多

提问者
hussain
被浏览
33
Davide Lorenzo MARINO 2019-07-03 21:53

您可以定义一个可能值的数组,并将其重写如下:

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."];

发布
问题

分享
好友

手机
浏览

扫码手机浏览