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 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Multidimensional / Associative arrays

    hi,

    I will try to explain what I am trying to do as best as I can, however I think it will be much easier to follow along if you can see a demo.

    Click here to view the demo.

    Okay, so I am trying to work with arrays.

    Code:
     
     var a = new Array('a1', 'a2');
     a['a1'] = 'a1value';
     a['a2'] = 'a2value';
     var b = new Array('b1', 'b2');
     b['b1'] = 'b1value';
     b['b2'] = 'b2value';
     var ab = new Array(a, b);
    When I try to access the array directly, I get the associative name ('a1', 'a2', 'b1', or 'b2') not the value ('a1value', 'a2value', 'b1value', 'b2value').

    Code:
     
     alert('Value of ab[0][1]:\n\n' + ab[0][1]);
     //should return 'a2value', but instead it returns 'a2'.
    At first I thought I was trying to store the value into the variable incorrectly.
    However, when I use for(value in array) loops, I get the associative name half the time, but the other half the time I can actually get the value. This means that the value is in the variable, but I don't know how to properly retrieve it.

    Here are my for loops:
    Code:
     
     for(value1 in ab){
      for(value2 in ab[value1]){
       alert(ab[value1][value2]);
      }
     }
    Another question, why is my second for loop looping 4 times through an array that only has a length of 2?

    This is very confusing, and I don't understand what is happening...
    Again, here is the demo to show you what I am trying to do.

    Any help is greatly appreciated!!!

    ~Brandon

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by 333qaz333 View Post
    Code:
     
     var a = new Array('a1', 'a2');
     a['a1'] = 'a1value';
     a['a2'] = 'a2value';
     var b = new Array('b1', 'b2');
     b['b1'] = 'b1value';
     b['b2'] = 'b2value';
     var ab = new Array(a, b);
    When I try to access the array directly, I get the associative name ('a1', 'a2', 'b1', or 'b2') not the value ('a1value', 'a2value', 'b1value', 'b2value').
    Elements created using associative syntax are stored entirely separately, they cannot be indexed numerically and their existence is not reflected in the length property.

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    I'll use array shorthand. Aka:
    Code:
    //longhand
    var x = new Array(1, 2, 3, . . . );
    //shorthand
    var x = [1, 2, 3, . . . ];
    *****************

    Okay, so
    Code:
    var a = ["a1", "a2"];
    
    //Same as 
    var a = [];
    a[0] = "a1";
    a[1] = "a2";

    Given
    Code:
    var a = ["a1", "a2"];
    a["a1"] = "a1value";
    a["a2"] = "a2value";
    var b = ["b1", "b2"];
    b["b1"] = "b1value";
    b["b2"] = "b2value";
    var ab = [a, b];
    a looks like:

    Code:
                   a
                   | 
       -----------------------------
       |      |        |           |
       |      |        |           |
      [0]    [1]      [a1]        [a2]
    ("a1") ("a2") ("a1value") ("a2value")
    b looks similar.

    ab looks like:
    Code:
        ab
        |
     --------
     |      |
    [0]    [1]
    (a)    (b)
     |      |
    ...    ...
    Trinithis

  • The Following 2 Users Say Thank You to Trinithis For This Useful Post:

    333qaz333 (12-30-2007), sybil6 (01-01-2008)

  • #4
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts
    THANK YOU TRINITHIS!!!!!!

    Thank you soo much... Your diagram/tree helped me out soo much.

    I owe you one...

    ~Brandon

  • #5
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts
    I have another question now...

    I know that this is kinda the opposite of what I was originally asking, but is there anyway for me to get the associative names (pretending that i don't know what they are)? Just curious...

    If there's not that's fine, it's not crucial and I can work fine without it, but I was just wondering if there was anyway to do that.

    Thank you again, you have saved me from countless hours of frusturation...

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    var aa = [1,2,3];
    aa.a="aaa";
    aa.b="bbb";

    var log="";
    for(var i in aa){
    log += ( "aa."+ i + "=" + aa[i]+ "\n" );
    }

    /* output:
    aa.0=1
    aa.1=2
    aa.2=3
    aa.a=aaa
    aa.b=bbb */
    Last edited by rnd me; 01-01-2008 at 08:25 PM. Reason: clarity
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Thank you for your reply, I have figured out what I did wrong. Instead of:

    Code:
    var a = new Array('a', 'b');
    a['a'] = 'avalue';
    a['b'] = 'bvalue';
     
    /*
    values in a are:
    a[1] = 'a';
    a[2] = 'b';
    a['a'] = 'avalue';
    a['b'] = 'bvalue';
    */
    I have to do this:

    Code:
    var a = [];
    a['a'] = 'avalue';
    a['b'] = 'bvalue';
     
    /*
    values in a are:
    a['a'] = 'avalue';
    a['b'] = 'bvalue';
    */
    I had made a mistake at first, but I have found the answer now.


  •  

    Posting Permissions

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