Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if an object is empty

Tags:

jquery

I have the following jQuery code:

var shown = $('div.slideshow').find('div.slide:visible');
var next = shown.next();

if(next == '') {                        
    console.log('empty');                       
}

Basically when the next comes back as empty like: [] I want to be able to detect this. How do I do it?

like image 682
Cameron Avatar asked Feb 14 '12 10:02

Cameron


People also ask

How do you know if an object has no value?

keys() is a static method that returns an Array when we pass an object to it, which contains the property names (keys) belonging to that object. We can check whether the length of this array is 0 or higher - denoting whether any keys are present or not. If no keys are present, the object is empty: Object.

How do you know if an object isn't empty?

return Object.keys(obj).length === 0 ;entries . This is typically the easiest way to determine if an object is empty.

How check if object is empty array?

Having confirmed that the variable is an array, now we can check the length of the array using the Array. length property. If the length of the object is 0, then the array is considered to be empty and the function will return TRUE. Else the array is not empty and the function will return False.


1 Answers

Use length property, which contains number of elements within jQuery object:

if(next.length === 0) {
    console.log('empty');
}

or:

if(!next.length) {
    console.log('empty');
}
like image 136
Tadeck Avatar answered Sep 28 '22 17:09

Tadeck