www.webdeveloper.com
Results 1 to 4 of 4

Thread: how do you detect empty object

  1. #1
    Join Date
    Feb 2010
    Posts
    49

    how do you detect empty object

    If kobe = {};

    what function of kobe would make {} evaluate to true and {pao: "gasol"} evaluate to false?

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    Interesting thread. Looks exactly like this one ->http://www.webdeveloper.com/forum/sh...d.php?t=193474

  3. #3
    Join Date
    Feb 2006
    Posts
    2,927
    If it was a property of the object, the object wouldn't be empty.
    You can check it from outside the object.

    Code:
    function isEmpty(obj){
        for(var p in obj){
            if(obj.hasOwnProperty(p)){
                return false
            }    
        }
        return true;
    }
    var o={};
    alert(isEmpty(o));


    /* returned value: (Boolean)
    false
    */

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    function isEmpty(obj){
        return JSON.stringify(obj)==="{}";
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles