views:

5891

answers:

9

After an AJAX request, sometimes my application may return an empty object, like:

var a = ({});

How can I check if that's the case?

+24  A: 

There's no easy way to do this. You'll have to loop over the properties explicitly:

function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }

    return true;
}
Christoph
I guess this is the correct answer, though it doesn't look good. I'll try to modify my application's response and see how that goes. Thanks!
falmp
This works fine, or more simply: function isEmpty(object) {for(var i in object) { return true; }return false;}
Nicholas Kreidberg
Shouldnt true and false be reversed in this function?
namtax
@namtax: no - the function is named `isEmpty()`, so it should return `false` if it has a property
Christoph
+4  A: 

1) Just a workaround. Can your server generate some special property in case of no data? For example:

var a = {empty:true};

Then you can easily check it in your AJAX callback code.

2) Another way to check it:

if (a.toSource() === "({})")  // then `a` is empty

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.

Thevs
`toSource()` is non-standard and doesn't work in IE or Opera (and potentially other browsers I didn't check)
Christoph
This is standard in ECMA-262. There are non-standard browsers though.
Thevs
@Thevs: perhaps you have a different copy of the current version of ECMA-262, but mine does not list a `toSource` property in section 15.2.4; according to MDC, it was introduced in JS1.3 (i.e. Netscape Navigator 4.06), but it's NOT in ECMA-262, 3rd edition!
Christoph
@Christoph: How do you think 3 other browsers would implement the same 'non-standard' feature if that wouldn't be a standard? :)
Thevs
@Thevs: well, at least 2 important browser vendors didn't implement it, so it's hardly a de-facto-standard, and as it's not in ECMA-262, it's not a real one either...
Christoph
...and as for how features get implemented across different browsers without being standard: ever heard of innovation and a thing called copying? It's how the standard was developed ion the first place (JavaScript and JScript predate ECMAScript)
Christoph
BTW, toSource() is implemented in IE _internally_, but nor exposed to outside. Think why?
Thevs
Sorry, - not exposed
Thevs
A: 

function isEmpty(obj) { for(var i in obj) { return false; } return true; }

That'll report also true, when, for instance, a JavaScript library extends `Object` with a method through the prototype chain, because that's enumerable and the `for in` statement loops through enumerable properties.
Török Gábor
+1  A: 

In addition to Thevs answer:

var o = {};
alert($.toJSON(o)=='{}'); // true

var o = {a:1};
alert($.toJSON(o)=='{}'); // false

it's jquery + jquery.json

Vacheslav
A: 

you must be binding this json to some dom element inspite of checking json best way is to make the the binding function handle such data

Praveen Prasad
+10  A: 

For those of you who have the same problem but uses jQuery, you can use jQuery.isEmptyObject.

Erik Töyrä
A: 

How about:

if (a.length != undefined) {
    // a is good, go ahead
}

This works for me.

yglodt
I don't get this. The OP is talking about an Object, not an Array. For example: var o = {"foo": "bar"}; alert(o.length); // undefined
Sam Dutton
+1  A: 

Here is another way:

a ={};

var b= {

        x: function() { return 'x' }
}

function isNotEmpty(obj) {
return (obj.__count__);
}

alert(isNotEmpty(a));

alert(isNotEmpty(b));

The aim of this function is to detect properties directly on obj.Zero(0) indicates no properties

Meryl
A: 

it's very simple

if is json, i always contains '{}'

so you should write :

"data" is string Json as : {'name':'phucvh', 'age':'21'}

if (data.charAt(0) == '{')

{

alert('is Json');

}

phucvh