Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Counting Object Properties?

    Hi there.
    Let's say I get a user-custom JavaScript object, with an unknown number of properties, all of them having custom, unknown names:
    Code:
    var object = {
        'foo': 1,
        'bar': 2,
        ...
        'baz': n
    }
    Now, let's say I would like to count the number of properties I have inside the object - can I do it? And if so, how?

    Plus, can I find out in some way what are the names of the properties, so I could use them?
    Last edited by pilau; 05-04-2007 at 02:21 PM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    for (var i in object){
      alert(i + "=" + object[i]);
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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