Home > Net >  Exclude property in checking empty properties
Exclude property in checking empty properties

Time:12-15

Below I have a code to check for empty property, it will return true if one property is empty. My question is how do we exclude other property on checking ?

for example I still wanna check for empty property but I dont want or I want to exclude properties like generalRealEstateConcernsorChallenges, majorPositiveAttributes , summary from checking.

So code below will only check empty property except generalRealEstateConcernsorChallenges, majorPositiveAttributes , summary. But I dont want to delete those property.

Thanks for help and idea.

code

const hasEmptyProperty = Object.values((this.dealDispositionFormFields)).some((v => v === null || v === ""))

object - this.dealDispositionFormFields

{
   
    "dealName": "12743-J",
    "summary": "A",
    "majorPositiveAttributes": "b",
    "generalRealEstateConcernsorChallenges": "c",
    "terminationPayment": 23,
    "effectiveDate": "2021-12-15T00:00:00",
    "brokerCommission": 232,
    "brokerRebate": 23233,
    "isPharmacyRestriction": false,
    "netEffectiveBrokerCommission": -23001,
}

CodePudding user response:

Try this. This will excude check in all the keys you pass in exclude array.

const excludeArray = ["generalRealEstateConcernsorChallenges", "majorPositiveAttributes" , "summary"];
const data = this.dealDispositionFormFields;

Object.keys(data).some((key) => {
  let value = data[key];
  return excludeArray.includes(key) ? false : (value === null || value === "");
})

Executable code for reference

let dealDispositionFormFields = {
  "dealName": "12743-J",
  "summary": "A",
  "majorPositiveAttributes": "b",
  "generalRealEstateConcernsorChallenges": "c",
  "terminationPayment": 23,
  "effectiveDate": "2021-12-15T00:00:00",
  "brokerCommission": 232,
  "brokerRebate": 23233,
  "isPharmacyRestriction": false,
  "netEffectiveBrokerCommission": -23001,
};

function checkEmptyKeys(data) {
  const excludeArray = ["generalRealEstateConcernsorChallenges", "majorPositiveAttributes", "summary"];

  return Object.keys(data).some((key, value) => {
    return excludeArray.includes(key) ? false : (data[key] === null || data[key] === "");
  });
}

console.log(checkEmptyKeys(dealDispositionFormFields));
dealDispositionFormFields.terminationPayment = "";
console.log(checkEmptyKeys(dealDispositionFormFields));

  • Related