0

i have to push the drug if either price is there in the object, I wanted to check if object is not undefined/null , does it make sense to these conditions ?

issue is its adding mailPrice when it is coming as {} , any idea ?

main.js

 _.forEach(drugs, function (drug) {
    if ((drug.retailPrice !== undefined && drugPrice.retailPrice !== null)  || (drug.mailPrice !== undefined && drug.mailPrice !== null)) {
          response.push(drug);
        }
});
hussain
  • 6,587
  • 18
  • 79
  • 152
  • 1
    what is your question ? are you facing any problem with it ? whether it makes sense or not depend how are the values are being used – Code Maniac Jun 10 '19 at 16:10
  • @CodeManiac updated question – hussain Jun 10 '19 at 16:11
  • 2
    Possible duplicate of [How to check for an undefined or null variable in JavaScript?](https://stackoverflow.com/questions/2559318/how-to-check-for-an-undefined-or-null-variable-in-javascript) – Heretic Monkey Jun 10 '19 at 16:14

1 Answers1

2

You can check if an object is empty using Object.keys:

const isEmpty = obj => Object.keys(obj).length === 0;

Now you should be able to do:

if (!isEmpty(drug)) {
  response.push(drug);
}
Jose A. Ayllón
  • 866
  • 6
  • 19