How to resolve this "Undetect TypeError: Unable to convert undefined or null to object"

My function:

function collect_que_ids(e) {
  var val = e.val();
  var data_lo = e.attr('data-lo');
  new_hash = {};
  new_hash[val] = data_lo;
  if(e.is(':checked')){
    if(checked_box_hash.includes(new_hash)){
      checked_box_hash;
    }else{
      checked_box_hash.push(new_hash);
    }
  }
  else{
    new_hash_key = Object.keys(new_hash)[0]
    new_hash_value = new_hash[new_hash_key]
    $.each(checked_box_hash, function(key, value){
      if (typeof Object.keys(value) !== 'nil') {
        current_key = Object.keys(value)
        if (current_key[0] == new_hash_key && value[current_key[0]] == new_hash_value) {
          checked_box_hash.splice(key, 1)
        }
      }
    })
  }
};

I get an error on this line.

if (typeof Object.keys(value) !== 'nil') {

It is necessary to solve the problem. please help do.

+4
source share
1 answer

Object.keys(value) returns an array and checks if it is undefined or empty do it

 if (typeof Object.keys(value) !== 'undefined' && Object.keys(value).length > 0) {
        // You have an array 
    }
+5
source

Source: https://habr.com/ru/post/1652802/


All Articles