Results 1 to 5 of 5

Thread: Shortest way to check if object is populated?

  1. #1
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default Shortest way to check if object is populated?

    Hi all,

    What is the shortest way to check if an object is empty or not? With an array, I just check that the length is greater than 0. Is there an equally simple way to check an object?

    Thanks!
    Last edited by jlizarraga; 01-20-2009 at 08:08 PM. Reason: resolved

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That question doesn't really make sense. No object (other than null) is truly 'empty': there are properties defined on Object.prototype.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    I found the answer:

    if (!Object.prototype.isEmpty)
    {
    Object.prototype.isEmpty = function() {
    for (var prop in this) {
    if (this.hasOwnProperty(prop)) return false;
    }
    return true;
    };
    }

    I am dealing with some JSON data where this is the only way I have of knowing whether or not to dive into the target object.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Why don't you just start the loop anyway? If there are no properties on the object, it won't do anything (but do make sure to check with hasOwnProperty()).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    Yeah this probably seems retarded without some backstory, lol.

    I do car websites (for my 9-5 anyway) and so I'm dealing with vehicle data that a web service spits out as a JSON object. When it comes time to display a vehicle's standard equipment, the various types of equipment are all objects.

    But sometimes a vehicle won't have a certain type of equipment, and rather than omitting the object for that type, it just comes back as a blank/empty. So I can't check for defined-ness.

    As for why I don't just start the loop anyway, I have to know how many key/value pairs are in the object before I use a loop to do something with each pair (which the server doesn't tell me). The data is going into a fixed number of columns (each column being part of the same overall list) but it is sometimes alphabetical so I use the number of pairs divided by the number of columns to figure out how many pairs to put in each column before moving on to the next column.

    So I first check to make sure the object has key/value pairs in it (the part I came for help on), then use the magic pair-per-column number to iterate through them once again, this time sending the data to the columns.
    Last edited by jlizarraga; 01-20-2009 at 10:54 PM. Reason: typo: "row" should have been "column"

Bookmarks

Posting Permissions

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