尝试从基于rejectMessage数组中的resolveCode的数组中删除元素,我觉得使用ES6或lodash可以更好。
有人可以帮我使用这种方法吗?
数据
const data = [
{
"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"
}
},
{
"drugName": "CALCIUM",
"mailPrice": {
"copayEmployer": "N/A",
"totalQuantity": "90.0"
},
"retailPrice": {
"copayEmployer": "N/A",
"totalQuantity": "30.0"
}
}
]
transform.js
function transformPrice(drugPrice) {
if (drugPrice.retailPrice.rejectMessage.length || drugPrice.mailPrice.rejectMessage.length ){
const retailRejecrMsg = drugPrice.retailPrice.rejectMessage;
const mailRejectMsg = drugPrice.mailPrice.rejectMessage;
const retailErr = isErrorPresent(retailRejecrMsg);
const mailErr = isErrorPresent(mailRejectMsg);
}
if(retailErr){
delete drugPrice.retailPrice;
}
if( mailErr){
delete drugPrice.mailPrice;
}
return drugPrice;
}
function isErrorPresent (price) {
const isError = function (element) {
const bRet = checkErrorCodes(element);
return (element.hasOwnProperty('settlementCodes') && bRet)
}
return price.some(isError);
}
function checkErrorCodes(el){
let bRet = false;
const errorCodes = [
10015,
2356,
225,
224,
99
]
for (const err of errorCodes){
if (err === ele.settlementCode){
bRet = true;
}
}
return bRet;
}
transformPrice(data);
预期结果
[{
"drugName": "Metformin",
"retailPrice": {
"copayEmployer": "N/A",
"totalQuantity": "30.0"
}
},
{
"drugName": "CALCIUM",
"mailPrice": {
"copayEmployer": "N/A",
"totalQuantity": "90.0"
},
"retailPrice": {
"copayEmployer": "N/A",
"totalQuantity": "30.0"
}
}
]
答案 0 :(得分:1)
您的意思是这样吗?
import _ from 'lodash';
const drugPrice = data.map(item => {
const errorCodes = [10015, 2356, 225, 224, 99];
const f = ["mailPrice.rejectMessage", "retailPrice.rejectMessage"];
f.forEach(path => {
const rejectMsg = _.get(item, path);
if (rejectMsg) {
const y = path.split(".").shift();
const hasCodeOrWrong = rejectMsg.find(i => {
return !!~errorCodes.indexOf(+i.settlementCode)
|| !!~i.settlementDesc.indexOf(':');
});
hasCodeOrWrong && delete item[y];
}
});
return item;
});