Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there an an easier way to see if an object is empty?

Tags:

javascript

Sure i can do:

var obj = {};
if(Object.keys(obj).length == 0)

but i was curious if there is a way to say:

var obj = {};
if(obj.hasKeys())

or even:

//already tested, and this doesnt work.  its true because it *is* something.
var obj = {};
if(!obj)
like image 415
Fallenreaper Avatar asked Dec 08 '25 22:12

Fallenreaper


1 Answers

function hasKeys(o) {
    for (var name in o)
        if (o.hasOwnProperty(name))
            return true;
    return false;
}
like image 66
squid314 Avatar answered Dec 11 '25 12:12

squid314



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!