简体   繁体   中英

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

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.

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.

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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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