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

时间:2019-06-06 16:40:05

标签: javascript arrays angular ecmascript-6

尝试省略包含字符串的元素,它仍然会返回包含<div formArrayName="addresses"> <!-- addresses is the name of the form array inside the owning form group tpForm --> <div [formGroupName]="0"> <!-- 0 is the index of form group inside the owning form array addresses --> <input formControlName="street"> <!-- street is the name of the form control inside the owning form group 0 --> 字符串的mailPrice,对此修复有任何想法吗?

数据

Not Covered

main.js

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": {}
};

预期输出

mailPrice在下面的响应中被省略,因为它的rejectMessage包含字符串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));

Not Covered

1 个答案:

答案 0 :(得分:1)

您在寻找这个吗: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);