如果对象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;
}
答案 0 :(得分:0)
如何调用is错误方法
const mailErr =isErrorPresent(mailPriceMsg).call();
或更改isErrorPresent方法
function isErrorPresent (price, element) {
const bRet = checkErrorCodes(element);
return (element.hasOwnProperty('settlementCode') && bRet)
}
答案 1 :(得分:0)
在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);
}
答案 2 :(得分:0)
return isError
仔细观察,您会发现isErrorPresent(price)(element)
实际上是在返回对函数的引用,而不是在调用函数后返回结果。
您可以像这样调用两个函数:function isErrorPresent (price, element) {
const bRet = checkErrorCodes(element);
return (element.hasOwnProperty('settlementCode') && bRet);
}
或这样定义函数:
isErrorPresent(price, element)
然后将其命名为{{1}}。
答案 3 :(得分:0)
您可以使用类似的东西:
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);
}
这就是我要解决的方法。希望这会有所帮助。
答案 4 :(得分:0)
我已经运行了您的代码,发现了几个问题:
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())