16
For jQuery
, I can tell if a Object
is empty as follows:
$.isEmptyObject({}); // true
$.isEmptyObject(window); // false
To find out if a array
is empty, we can do the same, but without jQuery
would look like this:
var arr = []
arr.length == 0; // True
However, and the Object
? How can I know if it is empty?
I figured I could do it like this:
Object.keys(obj).length == 0;
But I didn’t think it was appropriate.
What are other possible ways to do this?
Observing: I would like the answer to be without the use of jQuery
and others, but only using pure javascript.
jQuery source: isEmptyObject:Function(a){var b;for(b in a)Return! 1;Return! 0}
– epx
I bet you gave a
$.isEmptyObject.toSource()
:)– Wallace Maxters
Opened with same editor :)
– epx
why not use typeof ? and check if it is Undefined
– Rod
typeof
will not serve. He returns would"object"
for all cases (for empty and non-empty)– Wallace Maxters