After an AJAX request, sometimes my application may return an empty object, like:
var a = ({});
How can I check whether that's the case?
After an AJAX request, sometimes my application may return an empty object, like:
How can I check whether that's the case? |
|||||
|
Pre-ECMA 5:
|
|||||
|
There's no easy way to do this. You'll have to loop over the properties explicitly:
If ECMAScript 5 support is available, you can use
|
|||||||||||||||||||||
|
For those of you who have the same problem but uses jQuery, you can use jQuery.isEmptyObject. |
|||||||||||||||||||||
|
You can use Underscore.js.
|
|||||||||||||
|
see http://bencollier.net/2011/04/javascript-is-an-object-empty/ |
|||||||||
|
This is my preferred solution:
|
|||||||||||||||||
|
Old question, but just had the issue. Including JQuery is not really a good idea if your only purpose is to check if the object is not empty. Instead, just deep into JQuery's code, and you will get the answer:
|
|||||
|
How about using JSON.stringify? It is almost available in all modern browsers.
|
|||||||||||||
|
EDIT: If you use any JSON library (f.e. JSON.js) then you may try JSON.encode() function and test the result against empty value string. |
|||||||||||||||||||||
|
I just ran into a similar situation. I didn't want to use JQuery, and wanted to do this using pure Javascript. And what I did was, used the following condition, and it worked for me.
For not equal to, use this : Check out this JSFiddle |
|||||||||||||
|
There is a simple way if you are on a newer browser.
|
|||||||||||||||||
|
I am using this.
Eg:
Update OR you can use the jQuery implementation of isEmptyObject
|
|||||
|
I've created a complete function to determine if object is empty. It uses Solution
Here's the Gist for this code. And here's the JSFiddle with demonstration and a simple test. I hope it will help someone. Cheers! |
|||||
|
jQuery have special function
Read more on http://api.jquery.com/jQuery.isEmptyObject/ |
|||
|
|
|||||
|
JSON class and it's functions (parse and stringify) are very usefull but has some problems with IE7 that you can fix it with this simple code http://www.json.org/js.html.
|
||||
|
If jQuery and the web browser is not available, there is also an isEmpty function in underscore.js.
Additionally, it does not assume the input parameter to be an object. For a list or string or undefined, it will also turn the correct answer. |
|||
|
My take:
Just, I don't think all browsers implement |
|||
|
Caveat! Beware of JSON's limitiations.
displays Beware!! obj is NOT empty! obj = { f:function(){} } JSON.stringify( obj ) returns {} |
|||
|
In addition to Thevs answer:
it's jquery + jquery.json |
|||
|
Sugar.JS provides extended objects for this purpose. The code is clean and simple: Make an extended object:
Check it's size:
|
|||
|
A simple loop:
|
|||
|
this one line code helps
Object.getOwnPropertyNames is implemented in ECMA-5. the above line works in older browsers with a fallback function. |
|||||
|
This will check the emptiness of String, Array or Object (Maps). Usage :
|
||||
|
As of jQuery 1.4
|
||||
|
If you are checking object's emptiness for going in some code block, add a break after first foreach round, the code looks as simple as if statement.
|
|||
|
I was returning an empty JSON response for an AJAX call and in IE8 jQuery.isEmptyObject() was not validating correctly. I added an additional check that seems to catch it properly.
|
|||
|
Another alternative is to use is.js (14kB) as opposed to jquery (32kB), lodash (50kB), or underscore (16.4kB). is.js proved to be the fastest library among aforementioned libraries that could be used to determine whether an object is empty. http://jsperf.com/check-empty-object-using-libraries Obviously all these libraries are not exactly the same so if you need to easily manipulate the DOM then jquery might still be a good choice or if you need more than just type checking then lodash or underscore might be good. As for is.js, here is the syntax:
Like underscore's and lodash's Under the hood this library is using
If you don't want to bring in a library (which is understandable) and you know that you are only checking objects (not arrays or strings) then the following function should suit your needs.
This is only a bit faster than is.js though just because you aren't checking whether it is an object. |
||||
|
You can define you own object prototype, just before its usage or at the beginning of your code. The definition should look like this:
Here is a usage example:
Enjoy! :-) |
|||
|
I have an easy(but not generic) solution for this scenario : if you know a specific property name for your object , then you can easily check if that property exists. This way you would know if that object is empty or not , and you wouldn't need to traverse all the properties or use a library. Let me give an example : there is an object that , if its not empty, must have a property named "myProperty". Then you can check it like :
this is not a generic solution but it's been doing all i need actually, as most times i know what to expect in an object that i'm performing an empty check. |
|||
|