hussain
hussain

Reputation: 7103

How to check undefined object properties?

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

Upvotes: 0

Views: 39

Answers (1)

Jose A. Ayllón
Jose A. Ayllón

Reputation: 886

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

Upvotes: 2

Related Questions