简体   繁体   English

如何根据对象值返回布尔值?

[英]How to return boolean based on object value?

Trying to return boolean value if object rejectMessage contains the code that i have specified in checkErrorCodes method. 如果object rejectMessage包含我在checkErrorCodes方法中指定的代码,则尝试返回布尔值。 it should return true if settlementCode matches its not returning boolean value it is retuning whole isError function. 如果settCode匹配其未返回的布尔值,它应该返回true它重新调整整个isError函数。 Any idea or better approach to have this done ? 有没有想法或更好的方法来完成这项工作?

transformPrice.js 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 Method isErrorPresent方法

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

  return isError;
}

checkErrorCodes method 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;
}

how about calling the is error method 如何调用is error方法

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

or change the isErrorPresent method 或更改isErrorPresent方法

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

In the function isErrorPresent you are never calling the isError function. 在函数isErrorPresent中,您永远不会调用isError函数。 To call it you should: 要打电话给你,你应该:

return isError(price)

this will call the function with the argument price so in your example this would look like: 这将使用参数价格调用函数,因此在您的示例中,这将看起来像:

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

  return isError(price);
}
What you are doing here is creating a function that returns another function: 你在这里做的是创建一个返回另一个函数的函数:
 function isErrorPresent (price) { const isError = function (element) { const bRet = checkErrorCodes(element); return (element.hasOwnProperty('settlementCode') && bRet) } return isError; } 

If you look closely, you can see that return isError is actually returning a reference to a function, and not the result of the function after it has been called. 如果仔细观察,可以看到return isError实际上是返回对函数的引用,而不是函数调用后的结果。

You could call the two functions like so: isErrorPresent(price)(element) 您可以像这样调用两个函数: isErrorPresent(price)(element)


or define the function like so: 或定义函数如下:

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

and then call it like isErrorPresent(price, element) . 然后将其称为isErrorPresent(price, element)

You can use something like that: 你可以使用这样的东西:

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);
}

That's the way I'd approach this. 这就是我接近这个的方式。 Hope this helps. 希望这可以帮助。

I have ran your code and found several issues: 我运行了你的代码并发现了几个问题:

  • isErrorPresent Method return isError method not a value isErrorPresent方法返回isError方法而不是值
  • in transformPrice method you has some issues such as: 在transformPrice方法中,您有一些问题,例如:
    • missing semicolon before If statement If语句之前缺少分号
    • rejectMessage is array you should access via index. rejectMessage是您应该通过索引访问的数组。
    • you did not check null for retailRejecrMsg and mailPriceMsg 你没有为retailRejecrMsgmailPriceMsg检查null

Also I don't know what do you want after check Error, so I add value into drugPrice.mailErr and drugPrice.retailErr. 我也不知道在检查错误后你想要什么,所以我在drugPrice.mailErr和drugPrice.retailErr中添加了值。

I have fixed, hope it help. 我已经修好了,希望有所帮助。

 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()) 

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

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