简体   繁体   English

检查 object 中是否存在键及其值

[英]check if key and its value in exists in object

an object(ie request.query) must contain these keys and with keys they must have truthy values一个对象(即 request.query)必须包含这些键,并且它们必须具有真实值

const invoice_query_params = [
                'invoice_id',
                'invoice_receipt',
                'invoice_status',
                'payment_id',
                'signature' 
            ];

Below function with the object as a parameter should check if that object contains every array element as a parameter and the value of it is not falsy.下面 function 以 object 为参数应检查 object 是否包含每个数组元素作为参数并且它的值不虚假。

const checkKeysAndValues = (yourObject) => invoice_query_params.every(param => yourObject.hasOwnProperty(param) && yourObject[param])

Just test hasOwnProperty and for truthy values of course this will exclude anything that evaluates as falsy and accept anything that evaluates as truthy.只需测试 hasOwnProperty 并且对于真实值当然这将排除任何评估为虚假的内容并接受任何评估为真实的内容。

const invoice_query_params = [
                'invoice_id',
                'invoice_receipt',
                'invoice_status',
                'payment_id',
                'signature' 
            ];
let ok = true; 
for(var i=0; i < invoice_query_params.lengh; i++){
  if (request.query.hasOwnProperty(invoice_query_params[i]){
     if(!request.query[invoice_query_param[i]]){
        ok = false; 
     } 
  }
}

if (ok){
  // all were truthy so do something.
}

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

相关问题 检查键是否存在于数组中并替换其值 - Check if key exists in an array and replace its value 检查值是否存在于对象中,如果没有返回键 - Check if value exists in an object, if not return key 检查对象键值是否存在,如果存在则获取另一个键值 - Check if object key value exists, if so get another key value 使用JavaScript检查对象中是否存在带有某些“值”的键 - check if a key exists with certain “value” exists in an object using Javascript 如何检查 URL 参数是否作为 JSON 键存在,然后显示其值 - How to check if URL parameter exists as a JSON Key and then show its value 检查键值是否存在于数组对象中,如果不存在具有相同键值的数组和短数组内的推对象 - Check key value exists in array object or not, if not exists push object inside array and short array with same key value 通过选择器检查对象是否存在,并使用jQuery或Javascript返回布尔值 - Check if an object exists by its selector, return a boolean value with jQuery or Javascript 检查对象中是否存在值 - Check if value exists in object 检查对象数组中是否存在具有特定键值的对象 - check if an object with a specific key value exists in an array of objects 检查对象中是否不存在任何键/值(使用纯js) - Check if an any key/value does not exists in an object (with pure js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM