【问题标题】:How to return boolean based on object value?如何根据对象值返回布尔值?
【发布时间】:2019-10-09 08:23:41
【问题描述】:

如果对象 rejectMessage 包含我在 checkErrorCodes 方法中指定的代码,则尝试返回布尔值。如果结算代码匹配它的不返回布尔值,它应该返回 true 它正在重新调整整个 isError 函数。有什么想法或更好的方法来完成这项工作吗?

transformPrice.js

function transformPrice(oldDrugPrice) {
      let drugPrice =  {
            "drugName": "Metformin",
            "mailPrice": {
                "copayEmployer": "N/A",
                "totalQuantity": "90.0",
                "rejectMessage": [{
                    "settlementCode": "99",
                    "settlementDesc": "Not Covered: Call us - System could not process your request. Call us at the toll-free number on your benefit ID card.||Sin cobertura: Llámenos - El sistema no pudo procesar su solicitud. Llame al número gratuito que figura en su tarjeta de identificación de beneficios."
                }]
            },
            "retailPrice": {
                "copayEmployer": "N/A",
                "totalQuantity": "30.0"
            }
        },

      if (drugPrice.retailPrice.rejectMessage || drugPrice.mailPrice.rejectMessage.length ){
        const retailRejecrMsg = drugPrice.retailPrice.rejectMessage;
        const mailPriceMsg = drugPrice.mailPrice.rejectMessage;
         const retailErr = isErrorPresent(retailRejecrMsg);
         const mailErr =isErrorPresent(mailPriceMsg);

      }

      return drugPrice;
    }

isErrorPresent 方法

function isErrorPresent (price) {
  const isError = function (element) {
    const bRet = checkErrorCodes(element);
    return (element.hasOwnProperty('settlementCode') && bRet)
  }

  return isError;
}

checkErrorCodes 方法

function checkErrorCodes(el){
  let bRet = false;
  const errorCodes = [
    10015,
    2356,
    225,
    224,
      99
  ]

  for (const err of errorCodes){

    if (err === el.settlementCode){

      bRet = true;
    }
  }
   return bRet;
}

【问题讨论】:

  • element 应该从哪里来?
  • 为什么settlementCode 不是一个整数?
  • @JonasWilms 我已经检查了每个零售/邮件价格rejectMessage 数组,查看结算代码是否与代码匹配。它应该来自元素
  • @depperm 这就是后端返回的方式
  • 我看到了几个问题:settlementCodes 与 settlementCode,使用 === 将字符串与数字进行比较。

标签: javascript arrays angular ecmascript-6


【解决方案1】:

我已经运行了你的代码,发现了几个问题:

  • isErrorPresent 方法返回 isError 方法不是值
  • 在 transformPrice 方法中,您会遇到一些问题,例如:
    • If 语句前缺少分号
    • rejectMessage 是您应该通过索引访问的数组。
    • 您没有检查 retailRejecrMsg 和 mailPriceMsg 的 null

另外我不知道检查错误后你想要什么,所以我在 drugPrice.mailErr 和 drugPrice.retailErr 中添加了值。

我已经解决了,希望对你有帮助。

function transformPrice() {
      let drugPrice =  {
            "drugName": "Metformin",
            "mailPrice": {
                "copayEmployer": "N/A",
                "totalQuantity": "90.0",
                "rejectMessage": [{
                    "settlementCode": "99",
                    "settlementDesc": "Not Covered: Call us - System could not process your request. Call us at the toll-free number on your benefit ID card.||Sin cobertura: Llámenos - El sistema no pudo procesar su solicitud. Llame al número gratuito que figura en su tarjeta de identificación de beneficios."
                }]
            },
            "retailPrice": {
                "copayEmployer": "N/A",
                "totalQuantity": "30.0"
            }
        };

      if (drugPrice.retailPrice.rejectMessage || drugPrice.mailPrice.rejectMessage.length ){
        const retailRejecrMsg = drugPrice.retailPrice.rejectMessage ? drugPrice.retailPrice.rejectMessage[0]: null;
        const mailPriceMsg = drugPrice.mailPrice.rejectMessage ? drugPrice.mailPrice.rejectMessage[0]: null;
        drugPrice.retailErr = !retailRejecrMsg ? true : isErrorPresent(retailRejecrMsg);
        drugPrice.mailErr = !mailPriceMsg ? true : isErrorPresent(mailPriceMsg);
      }

      return drugPrice;
}

function isErrorPresent (price) {
    const bRet = checkErrorCodes(price);
    return (price.hasOwnProperty('settlementCode') && bRet)
}

function checkErrorCodes(el){
  let bRet = false;
  const errorCodes = [
    10015,
    2356,
    225,
    224,
    99
  ]

  for (const err of errorCodes){
    if (err === el.settlementCode){
      bRet = true;
    }
  }
   return bRet;
}
console.log(transformPrice())

【讨论】:

    【解决方案2】:

    你可以使用类似的东西:

    function isErrorPresent (obj) {
    
      if(checkErrorCodes(obj) && obj.hasOwnProperty('settlementCode')){ //error code checked
        return obj; //return obj
      }
      return false; //no error found
    
    }
    
    function checkErrorCodes(el){
      const errorCodes = [
        10015,
        2356,
        225,
        224,
          99
      ]
      return errorCodes.includes(el.settlementCode);
    }
    
    

    这就是我处理这个问题的方式。希望这会有所帮助。

    【讨论】:

      【解决方案3】:
      您在这里所做的是创建一个返回另一个函数的函数:
      function isErrorPresent (price) {
        const isError = function (element) {
          const bRet = checkErrorCodes(element);
          return (element.hasOwnProperty('settlementCode') && bRet)
        }
      
        return isError;
      }
      

      如果你仔细观察,你会发现return isError实际上是返回一个函数的引用,而不是函数被调用后的结果。

      你可以像这样调用这两个函数:isErrorPresent(price)(element)


      或者像这样定义函数:

      function isErrorPresent (price, element) {
          const bRet = checkErrorCodes(element);
          return (element.hasOwnProperty('settlementCode') && bRet);
      }
      

      然后将其称为isErrorPresent(price, element)。

      【讨论】:

        【解决方案4】:

        在函数 isErrorPresent 中,您永远不会调用 isError 函数。要调用它,您应该:

        return isError(price)
        

        这将调用带有参数 price 的函数 所以在你的例子中,这看起来像:

        function isErrorPresent (price) {
          const isError = function (element) {
            const bRet = checkErrorCodes(element);
            return (element.hasOwnProperty('settlementCode') && bRet)
          }
        
          return isError(price);
        }
        

        【讨论】:

          【解决方案5】:

          调用is错误方法怎么样

          const mailErr =isErrorPresent(mailPriceMsg).call();
          

          或更改 isErrorPresent 方法

          function isErrorPresent (price, element) {
              const bRet = checkErrorCodes(element);
              return (element.hasOwnProperty('settlementCode') && bRet)
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-08-23
            • 1970-01-01
            • 1970-01-01
            • 2020-05-22
            • 1970-01-01
            • 1970-01-01
            • 2019-06-10
            • 2011-12-08
            相关资源
            最近更新 更多