Created
September 16, 2016 04:37
-
-
Save prinsss/ac924ca2952a93667f8211b7b626a2fd to your computer and use it in GitHub Desktop.
Check if given object empty.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function isEmpty(obj) { | |
| // null and undefined are "empty" | |
| if (obj == null) return true; | |
| // Assume if it has a length property with a non-zero value | |
| // that that property is correct. | |
| if (obj.length > 0) return false; | |
| if (obj.length === 0) return true; | |
| // If it isn't an object at this point | |
| // it is empty, but it can't be anything *but* empty | |
| // Is it empty? Depends on your application. | |
| if (typeof obj !== "object") return true; | |
| // Otherwise, does it have any properties of its own? | |
| // Note that this doesn't handle | |
| // toString and valueOf enumeration bugs in IE < 9 | |
| for (var key in obj) { | |
| if (hasOwnProperty.call(obj, key)) return false; | |
| } | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment