简体   繁体   English

检查 object 中是否存在密钥以及它是否具有值

[英]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.我们如何检查键并将其与数据 object 进行比较,如果键数组中的一个或多个键在 object 数据中不存在,或者如果它存在或键存在并且键值没有值或 null 或未定义则返回 false否则返回真。

For example keys has a key summary and it exists on object data but the value is empty so it should return false;例如 keys 有一个 key summary 并且它存在于 object 数据上但是值为空所以它应该返回 false;

I've tried Object.keys and used includes but cant seem to work it out, maybe someone has an idea.我试过 Object.keys 并使用了 includes 但似乎无法解决,也许有人有想法。 Thanks.谢谢。

#currentCode #current代码

 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 #sample object 数据

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

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)

You could use every() with hasOwnProperty and additional checks for empty strings etc您可以将every()hasOwnProperty一起使用,并对空字符串等进行额外检查

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

My approach would be to check whether all keys are present in data with the help of .every .我的方法是在.every的帮助下检查data中是否存在所有keys
Also non-strict != will check if certain key contain neither null nor undefined同样非严格的!=将检查某个是否既不包含null也不包含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));

According to mdn,根据 mdn,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM