Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

check if keys exist in object and if it has value

How do we check the keys and compare it to the data object , if one or more keys from the keys array does not exist in object data or if it exist or key exists and the key value has no value or null or undefined then return false else return true.

For example keys has a key summary and it exists on object data but the value is empty so it should return false;

I’ve tried Object.keys and used includes but cant seem to work it out, maybe someone has an idea. Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#currentCode

 const sample =  Object.entries(sampleOject).some((value) => {
          return keys.includes(value[0]) ? false : (value[1] === null || value[1] === "");
      })

Thanks.

#keys

const keys =  [
    'summary',
    'targetRecdate',
    'majorPositiveAttributes',
    'generalRealEstateConcernsorChallenges',
    'terminationPayment',
    'effectiveDate',
    'brokerCommission',
    'brokerRebate',
    'netEffectiveBrokerCommission']

#sample object data

{
    "dealDispositionType": "A",
    "majorPositiveAttributes": "a",
    "terminationPayment": "31",
    "netEffectiveBrokerCommission": -12189,
    "brokerCommission": "123",
    "brokerRebate": "12312",
    "isPharmacyRestriction": 0,
    "periodOfRestriction": null,
    "pharmacyRestrictionDate": null,
    "targetRecdate": "2022-10-20",
    "isLandLordConsent": false,
    "summary: ""
}

>Solution :

You could use every() with hasOwnProperty and additional checks for empty strings etc

const result = keys.every(key => {
    return data.hasOwnProperty(key) && data[key] !== ''
}, {});
const keys =  [
    'summary',
    'targetRecdate',
    'majorPositiveAttributes',
    'generalRealEstateConcernsorChallenges',
    'terminationPayment',
    'effectiveDate',
    'brokerCommission',
    'brokerRebate',
    'netEffectiveBrokerCommission'
];

const data = {
    "dealDispositionType": "A",
    "majorPositiveAttributes": "a",
    "terminationPayment": "31",
    "netEffectiveBrokerCommission": -12189,
    "brokerCommission": "123",
    "brokerRebate": "12312",
    "isPharmacyRestriction": 0,
    "periodOfRestriction": null,
    "pharmacyRestrictionDate": null,
    "targetRecdate": "2022-10-20",
    "isLandLordConsent": false,
    "summary": ""
};

const result = keys.every(key => {
    return data.hasOwnProperty(key) && data[key] !== ''
}, {});

console.log(result); // False
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading