gpt4 book ai didi

javascript - 如何检查数组元素是否包含字符串?

转载 作者:行者123 更新时间:2023-12-02 23:31:27 25 4
gpt4 key购买 nike

尝试省略包含字符串的元素,但它仍然返回包含 Not Covered 字符串的 mailPrice ,对修复有任何想法吗?

数据

const drug = {
"isBrand": false,
"drugName": "Atorvastatin Calcium",
"drugStrength": "80mg",
"drugForm": "Tablet",
"mailPrice": {
"totalQuantity": 90,
"rejectMessage": [{
"settlementCode": "99",
"settlementDesc": "Not Covered Sorry, the system is temporarily:Lo sentimos,Intente(Código de error 85)"
}]
},
"retailPrice": {
"totalQuantity": 30,
"rejectMessage": [{
"settlementCode": "99",
"settlementDesc": "Sorry, the system is temporarily:Lo sentimos,Intente(Código de error 85)"
}]
},
"specialtyPrice": {}
};

main.js

const priceFilterHandler = (item) => {
const retailHasCode = findErrCode(item.retailPrice && item.retailPrice.rejectMessage);
const mailHasCode = findErrCode(item.mailPrice && item.mailPrice.rejectMessage);
if (retailHasCode) {
delete item.retailPrice;
}

if (mailHasCode) {
delete item.mailPrice;
}

return item;
}


const findErrCode = (data) => data && data.some((item) =>

item.settlementDesc.includes(!'Not Covered')
);

console.log(priceFilterHandler(drug));

预期输出

下面的响应中省略了 mailPrice,因为它的拒绝消息包含字符串 Not Covered

{
"isBrand": false,
"drugName": "Atorvastatin Calcium",
"drugStrength": "80mg",
"drugForm": "Tablet",
"retailPrice": {
"totalQuantity": 30,
"rejectMessage": [{
"settlementCode": "99",
"settlementDesc": "Sorry, the system is temporarily:Lo sentimos,Intente(Código de error 85)"
}]
},
"specialtyPrice": {}
};

最佳答案

您在找这个吗:https://jsfiddle.net/5cnqwfgu/1/

Object.entries(drug).forEach(entry => {
if(typeof entry[1] === "object") {
if(entry[1]['rejectMessage'] && entry[1]['rejectMessage'].length > 0 && entry[1]['rejectMessage'][0]['settlementDesc'].includes('Not Covered')){
delete drug[entry[0]];
}
}
});

console.log(drug);

关于javascript - 如何检查数组元素是否包含字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56481820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com