![](/img/trans.png)
[英]how to return boolean value based on condition check in array object javascript
[英]How to return boolean based on object value?
如果object rejectMessage包含我在checkErrorCodes方法中指定的代碼,則嘗試返回布爾值。 如果settCode匹配其未返回的布爾值,它應該返回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;
}
如何調用is error方法
const mailErr =isErrorPresent(mailPriceMsg).call();
或更改isErrorPresent方法
function isErrorPresent (price, element) {
const bRet = checkErrorCodes(element);
return (element.hasOwnProperty('settlementCode') && bRet)
}
在函數isErrorPresent中,您永遠不會調用isError函數。 要打電話給你,你應該:
return isError(price)
這將使用參數價格調用函數,因此在您的示例中,這將看起來像:
function isErrorPresent (price) {
const isError = function (element) {
const bRet = checkErrorCodes(element);
return (element.hasOwnProperty('settlementCode') && bRet)
}
return isError(price);
}
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)
。
你可以使用這樣的東西:
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);
}
這就是我接近這個的方式。 希望這可以幫助。
我運行了你的代碼並發現了幾個問題:
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())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.