Is there a better way to check if an object is empty? I'm using this:
function isObjEmpty(obj)
{
for (var p in obj) return false;
return true;
}
If you're looking for a one-liner, consider Object.keys
:
var isEmpty = !Object.keys(obj).length;
Your current method is dangerous, since it will always return false when Object.prototype
has been extended: http://jsfiddle.net/Neppc/
Another option is built into jQuery: jQuery.isEmptyObject(obj)
Edit: Interestingly, that implementation is the same as your code in the question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With