In my code, I have a loop on rxDetails that is supposed to add a new field payAmount
if any rxNumber matches with the data. However, when I run the forEach loop as shown below, it always misses the rxNumber 15131503
in the return. I'm not sure what I did wrong here. I noticed that the forEach loop is skipping one of the elements but I can't figure out why.
Here is the data:
const rxDetails = [
{
"drugName": "TRILIPIX 135MG CPDR",
"rxNumber": "15131523",
"lldIndicator": "N"
},
{
"drugName": "GILENYA 0.5MG CAPS",
"rxNumber": "15131519",
"lldIndicator": "N"
},
{
"drugName": "JAKAFI 5MG TABS",
"rxNumber": "15131503",
"lldIndicator": "Y"
},
{
"drugName": "FENOFIBRATE MICRONIZED 134MG CAPS",
"rxNumber": "15131510",
"lldIndicator": "N"
},
{
"drugName": "LIPITOR 10MG TABS",
"rxNumber": "15131506",
"lldIndicator": "N"
},
{
"drugName": "KEFLEX 750MG CAPS",
"rxNumber": "15131522",
"lldIndicator": "N"
}
]
const data = [{
"drugName": "TRILIPIX 135MG CPDR",
"rxNumber": "15131523",
"lldIndicator": "N",
"payAmount": "10"
},
{
"drugName": "GILENYA 0.5MG CAPS",
"rxNumber": "15131519",
"lldIndicator": "N",
"payAmount": "8"
},
{
"drugName": "METFORMIN",
"rxNumber": "15425789",
"lldIndicator": "Y",
"payAmount": "0.50"
},
{
"drugName": "FENOFIBRATE MICRONIZED 134MG CAPS",
"rxNumber": "15131510",
"lldIndicator": "N",
"payAmount": "2.56"
},
{
"drugName": "LIPITOR 10MG TABS",
"rxNumber": "15131506",
"lldIndicator": "N",
"payAmount": "7.76"
},
{
"drugName": "KEFLEX 750MG CAPS",
"rxNumber": "15131522",
"lldIndicator": "N",
"payAmount": "17.88"
}
]
The issue seems to be in the main.ts file:
private getDrugsLastPrice(rxDetails: any, data: any) {
let isDrugFound: boolean = false;
const drugsArray: any = [];
rxDetails.forEach((item: any) => {
for (const element of data) {
if (item.rxNumber === element.rxNumber) {
isDrugFound = true;
const singleDrug = {
rxNumber: item.rxNumber,
lldIndicator: item.lldIndicator,
drugName: item.drugName,
payAmount: element.payAmount
};
drugsArray.push(singleDrug);
}
}
if (!isDrugFound) {
drugsArray.push(item);
}
});
return drugsArray;
}
getDrugsLastPrice(rxDetails,data);
The expected output should be:
[{
"drugName": "TRILIPIX 135MG CPDR",
"rxNumber": "15131523",
"lldIndicator": "N",
"payAmount": "10"
},
{
"drugName": "GILENYA 0.5MG CAPS",
"rxNumber": "15131519",
"lldIndicator": "N",
"payAmount": "8"
},
{
"drugName": "JAKAFI 5MG TABS",
"rxNumber": "15131503",
"lldIndicator": "Y"
},
{
"drugName": "FENOFIBRATE MICRONIZED 134MG CAPS",
"rxNumber": "15131510",
"lldIndicator": "N",
"payAmount": "2.56"
},
{
"drugName": "LIPITOR 10MG TABS",
"rxNumber": "15131506",
"lldIndicator": "N",
"payAmount": "7.76"
},
{
"drugName": "KEFLEX 750MG CAPS",
"rxNumber": "15131522",
"lldIndicator": "N",
"payAmount": "17.88"
}
]