hussain
hussain

Reputation: 7103

How to check if array elements contain string?

Trying to omit the element that contain string it is still returning mailPrice that contains Not Covered string , Any idea about the fix ?

data

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));

expected output

mailPrice is omitted in below response because its rejectMessage contain string 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": {}
    };

Upvotes: 0

Views: 68

Answers (1)

Gourishankar
Gourishankar

Reputation: 956

Are you looking for this: 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);

Upvotes: 1

Related Questions