Web Analytics Made Easy -
StatCounter How do you add associative arrays to an array? - CodingForum


No announcement yet.

How do you add associative arrays to an array?

  • Filter
  • Time
  • Show
Clear All
new posts

  • How do you add associative arrays to an array?


    Does anyone know how to do the following.
    I've tried a bunch of approaches, and it's driving me nuts!
    Any help is appreciated.

    Overall, I'm trying to set a whole mess of values in an array so they can be accessed like this:
    value = myArray[key1][key2][key3][key4]

    This is a smaller test, just trying to do:
    value = myArray[key1][key2]

    // initialize some vars
    var myArray = new Array();
    var assocArray1 = new Array();
    var assocArray2 = new Array();
    var key1 = 0;      //an integer key
    var key2 = 'strKey1';     //str key
    var key2_1 = 'strKey2';     //str key
    assocArray1 [key2] = 3.142;     //any value for test
    assocArray2 [key2_1] = 2.718;     //any value for test
    //can we make an array of associative arrays?
    myArray.push([]);    //push the first element onto the array
    myArray[key1] = assocArray1;    //set the array's first element to an associative array that we made earlier
    alert(myArray[key1][key2]);    //see if we can find our value in the array of arrays -- works, returns: "3.142"
    //flushed with success, try adding another associative array into the first element
    myArray[key1].push(assocArray2);    //push an associative array onto the array, after the first associative array
    alert(myArray[key1][key2_1]);    //see if we can find our value in the array of arrays -- should return: "2.718"
                                  //but it actually returns "undefined"
    Last edited by cycleops; Oct 2, 2006, 07:13 PM.

  • #2
    You need to understand how javascript works.

    Arrays are ONLY numerically indexed in javascript. There are no associative arrays. What you believe to be associative arrays are actually objects. It's confusing because javascript allows you to use array notation to access object properties. See this:

    var x = {};
    x.pink = 'elephant';
    alert(x['pink']); // alerts elephant
    So you're making a huge mess of things by starting off with an empty array ([]) and then adding properties to the object. Because JavaScript objects can be expanded, when you do this:
    var x = [];
    x['blue'] = 'sky';
    You create a property on x called blue (x.blue).

    It can get confusing if you're unaware of this.

    So how do you achieve what you want?

    var x = {};
    x['key1'] = {};
    x.key1['key2'] = 'value';
    alert(x.key1.key2); // alerts value
    So if you use this new knowledge, you should be able to achieve what you want.


    • #3
      Originally posted by Beagle View Post
      You need to understand how javascript works.

      Arrays are ONLY numerically indexed in javascript.
      I disagree. Firstly, Arrays are just Objects too. Therefore the bracket notation is equally valid as it is on objects. Furthermore:
      var test = {
          toString: function() {
              alert("toString() method has been called.");
              return 0;
      var myArray = [1,2,3];
      If Arrays were looking for integer indices, why does it natively invoke the toString() method on any variable which isn't a primitive integer?


      • #4
        Arrays are indeed objects, but objects ARE NOT arrays. Arrays, come with a .push method, and a .length property, so they are certainly objects. But, an associative array is not an array at all, but instead a generic object. An "associative array" doesn't have a .push array, because it's only meaningful to a numerically indexed array. Further, you cannot get an accurate length of an "associative array" using .length, because it only counts the indexed entries.

        I did not actually say, nor did I mean to imply, that the only members of an array are the numerically indexed ones. What I said was that there's no such thing as an associative array, only numerically indexed arrays and objects (numerically indexed arrays happen to be an object). What's important to note is that the OP believes he is using arrays and is therefore restricting his ideas for a possible solution to his problem. By revealing that associative arrays aren't arrays at all, and don't need to be declared with [] or "new Array()", I was hoping to show the OP a new way of looking at, and potentially solving, the problem.