0

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": {}
    };
15
  • checkout typeof keyword you should be able to add a condition like if(typeof val === 'string') Commented Jun 6, 2019 at 16:42
  • You sure you want to check the rejectMessage includes that? and Not settlementDesc or something else? Commented Jun 6, 2019 at 16:47
  • @TKol so each element in rejectMessage will have settlementDesc we need to check if it contains Not Covered then returns true Commented Jun 6, 2019 at 16:48
  • @TKoL ok i have updated my question by checking item.settlementDesc.includes(!'Not Covered') it returns mailPrice as well it should not as i mentioned in expected output Commented Jun 6, 2019 at 16:53
  • iitem.settlementDesc.includes(!'Not Covered') there is a typo here Commented Jun 6, 2019 at 16:55

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@hussain..please accept if this solves your problem !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.