Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
94
rated 0 times [  98] [ 4]  / answers: 1 / hits: 50429  / 8 Years ago, mon, august 29, 2016, 12:00:00

My function is :



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 am getting error on this line.



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


Need to resolve it. please help to do.


More From » jquery

 Answers
28

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



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

[#60890] Thursday, August 25, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breap

Total Points: 606
Total Questions: 96
Total Answers: 108

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
;