简体   繁体   English

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

[英]How to check same string value for multiple properties and shorthand if statement?

Basically if any of the value in mailPrice contain N/A or 'n/a` trying to return true its not happening looks like i am missing something , Also if there is any better approach instead of writing big if condition i would appreciate ? 基本上,如果mailPrice任何值包含N/A或'n N/A '试图返回true,则它似乎没有出现,这似乎是我缺少了什么;如果有条件的话,还有什么更好的方法而不是大写吗?

main.js 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)); 

You can define an array of possible values and rewrite it as follow: 您可以定义一个可能值的数组,并将其重写如下:

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) {
  ...
}

This can be useful if you have different conditions that can be applied for na, for example 例如,如果您可以将不同的条件应用于na,这将很有用。

 const na = ["N/A", "n/a", "na", "NA", "n.a."];

You can use Object.keys and some : 您可以使用Object.keyssome

Object.keys(drug.mailPrice)
      .some(prop => drug.mailPrice[prop].toLowerCase() == 'n/a');

Edit based on your comment: If you want to exclude certain properties, you can use filter 根据您的评论进行编辑:如果要排除某些属性,则可以使用filter

Object.keys(drugz.mailPrice)
    .filter(prop => !['memberCopayAmount', 'totalDrugCost'].includes(prop))
    .some(prop => drugz.mailPrice[prop].toLowerCase() == 'n/a');

You could try this way using .some 您可以使用.some尝试这种方式
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/some

const priceFilterHandler = (item) => {
  return Object.keys(item.mailPrice).some(prop => item.mailPrice[prop].toLowerCase() === 'n/a');
};

console.log(priceFilterHandler(drug));

Your code works fine but you can loop through the object and then check if the object contains N/A or n/a 您的代码工作正常,但是您可以遍历对象,然后检查对象是否包含N / A或n / a

      // loop through the mailPrice object
      Object.entries(item.mailPrice).forEach(entry => {
         if (entry[1].toString().toLowerCase() === 'n/a') {
          bRet = true;
          retrun;
        }
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM