Results 1 to 2 of 2

Thread: Javascript Does Not Support Associative Arrays...

  1. #1
    Join Date
    Jul 2008
    Posts
    40
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Lightbulb Javascript Does Not Support Associative Arrays...

    Javascript Does Not Support Associative Arrays
    An associative array is an array which uses a string instead of a number as an index.

    Code:
    var normalArray = []; normalArray[1] = 'This is an enumerated array'; alert(normalArray[1]); // outputs: This is an enumerated array var associativeArray   = []; associativeArray['person'] = 'John Smith'; alert(associativeArray['person']); // outputs: John Smith
    Javascript does not have, and does not support Associative Arrays. However... All arrays in Javascript are objects and Javascript's object syntax gives a basic emulation of an associative Array. For this reason the example code above will actually work. Be warned that this is not a real array and it has real pitfals if you try to use it. The 'person' element in the example becomes part of the Array object's properties and methods, just like.length,.sort (),.splice(), and all the other built-in properties and methods.
    You can loop through an object's properties with the following loop...

    Code:
    var associativeArray = []; associativeArray["one"] = "First"; associativeArray["two"] = "Second"; associativeArray["three"] = "Third"; for (i in associativeArray) { document.writeln(i+':'+associativeArray[i]+', '); // outputs: one:First, two:Second, three:Third };
    In the above example, associativeArray.length will be zero because we didn't actually put anything into the Array, we put it into associativeArray's object. "associativeArray[0]" will be undefined.
    The loop in the above example will also pick up any methods, properties, and prototypes which have been added to the array and not just your data. A lot of problems people have with the Prototype library is that their "associative arrays" break because Prototype adds a few useful Prototype functions to the Array object and "for i in x" loops pick up those additional methods. That's the pitfal of using Array/objects as a poor man's associative array.
    As a final example, the previous code will "work" regardless of whether you define associativeArray as an Array ([]), an Object({}), a Regular Expression (//), String(""), or any other Javascript object.
    The bottom line is -- don't try to use associative arrays, code for what they are -- object properties, not Arrays.
    Array Methods Reference
    Since Javascript Arrays are modified objects, each and every Array you create has a few core methods. What's really interesting is that some of these methods implement basic data structures you'd normally have to write yourself such as stacks (push, pop) and queues (shift, unshift).
    Last edited by ddadmin; 07-26-2008 at 07:32 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In javascript (if I'm following you), what you call an associative array can be accomplished using an object:

    Code:
    var assoc = {};
    assoc['blah'] = "I'm Blah";
    assoc['blue'] = "I'm Blue";
    
    alert(assoc.blue);
    Objects can also be defined (the preferred method in my opinion):

    Code:
    var assoc = {
    blah : "I'm Blah",
    blue : "I'm Blue"
    };
    
    alert(assoc.blue);
    I prefer this second method because, in more complex code it offers greater utility with less written code.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •