Home > database >  check if keys exist in object and if it has value
check if keys exist in object and if it has value

Time:10-06

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.

#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: ""
}

CodePudding user response:

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

CodePudding user response:

I just optimized your code.

const sample =  Object.entries(sampleOject).map(([key, value]) => {
   return keys.includes(key) ? value ? true : false : false;
})

...

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

const obj = {
    "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": "test"
}

let arr = [];

const result = Object.entries(obj).map(([key, val]) => {
    if (keys.includes(key)) {
        if ((val !== '') && (val !== 'undefined') && (val !== 'null') ) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
})

const getValue = result.includes(true);

console.log(getValue)

CodePudding user response:

My approach would be to check whether all keys are present in data with the help of .every.
Also non-strict != will check if certain key contain neither null nor undefined

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 check = (obj, keys) => keys.every((key) =>  
    key in obj && obj[key] != undefined);

console.log(check(data, keys));

CodePudding user response:

According to mdn,

const car = { make: 'Honda', model: 'Accord', year: 1998 };
console.log('make' in car); // output: true
  • Related