Seeking to optimize the code by using a filter instead of lodash _forEach. However, the current filter code is not producing the desired result. Any insights on what might be incorrectly implemented here?
main.js
const response = [];
const data = [{
"isBrand": true,
"drugName": "Lipitor",
"specialtyPrice": {}
},
{
"isBrand": false,
"drugName": "Atorvastatin Calcium",
"drugStrength": "80mg",
"drugForm": "Tablet",
"mailPrice": {
"totalQuantity": 90,
"rejectMessage": [{
"settlementCode": "99",
"settlementDesc": "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": {}
}
];
_.forEach(data, function(drug) {
if (drug.retailPrice !== undefined || drug.mailPrice !== undefined) {
response.push(drug);
}
});
const filterItems = data.filter(item => item.retailPrice && item.mailPrice)
console.log(filterItems);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
Expected Outcome
[{
"isBrand": false,
"drugName": "Atorvastatin Calcium",
"drugStrength": "80mg",
"drugForm": "Tablet",
"mailPrice": {
"totalQuantity": 90,
"rejectMessage": [{
"settlementCode": "99",
"settlementDesc": "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": {}
}
];