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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How to create nested objects array in javascript

    Hi,
    I am a begginer in javascript, please help on below
    I want to create an object with nested objects array like following:

    var country{
    var states=new array();
    }
    var state
    {
    city,pincode
    }

    this should be called like
    var city= country[0].state[0].city;


    any idea will be appreciated

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    How about something like this, instead of using numbers?
    Code:
    var countries = {
        "USA" : {
             "Alabama" : {
                    "Birmingham" : 371,
                    "Montgomery" : 8771 },
             "Alaska" : {
                    "Anchorage" : 9918,
                    "Nome" : 8171 },
              "Wyoming"  : {
                    "Casper" : 718,
                    "Cody" : 888 }
            },
        "Canada" : [ 
            "Alberta" : {
                  "Calgary" : "Z871" },
            "British Columbia" {
                  "Vancouver" : "B88C",
                  "Victoria" : "C99A" },
          ...
    };
    
    var pin = countries["USA"]["Alaska"]["Nome"];
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    suresh.maurya (01-17-2012)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks Pedant for your reply,

    this is exactly what i was trying to create, but i want to fill data in this countries object dynamically not hard coded.

    which is my real problem.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    No problem.

    You could do it one step at a time, like this:

    Code:
    var countries = [ ];
    countries["USA"] = [ ];
    countries["USA"]["Alaska"] = [ ];
    countries["USA"]["Alaska"]["Anchorage"] = 7183;
    countries["USA"]["Alaska"]["Nome"] = 9378;
    or in variations on that. Example:
    Code:
    var california = [ ];
    california["Los Angeles"] = 91010;
    california["San Francisco"] = 95004;
    
    var texas = [ ];
    texas["Dallas"] = 71822;
    texas["San Antonio"] = 78811;
    
    ...
    
    var usa = [ ];
    usa["California"] = california;
    usa["Texas"] = texas;
    ...
    
    var countries = [ ];
    countries["USA"] = usa;
    ...
    Or variations in between.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    suresh.maurya (01-17-2012)

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks , this is really helpful to me.

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    You should use {} instead of [] when creating the object in the first place. This will create a JSON compatible object ... if you ever plan to convert it using JSON.stringify() etc

    Example:
    Code:
    var countries = {};
    countries["USA"] = {};
    countries["USA"]["Alaska"] = {};
    countries["USA"]["Alaska"]["Anchorage"] = 7183;
    countries["USA"]["Alaska"]["Nome"] = 9378;
    
    alert(JSON.stringify(countries));   // will give you a valid JSON string representation

  • Users who have thanked devnull69 for this post:

    suresh.maurya (01-19-2012)


  •  

    Tags for this Thread

    Posting Permissions

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