SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Associative Arrays

    I have this extremely simple script:

    Code:
    foo = [];
    foo['bar'] = 'bas';
    alert(foo.length);
    Why do I always get '0' in the alert box?

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    try: foo = new Array();

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stymiee
    try: foo = new Array();
    thanks , but still zero

  4. #4
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No such thing as associative arrays in JS, a['bar'] = 1 actually means that you are setting the "bar" expando-property of a (a.bar) to 1. Unfortunately you're out of luck here and you're just gonna have to find another way around it.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that would make a non-associative array.

  6. #6
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try to be objective in regard to associative arrays:

    var foo={a:'abc',b:'efg',c:'hij'};
    var foo_length=0;
    for(var x in foo){
    foo_length++;
    }
    alert(foo_length+'\n'+foo['a']+'\n'+foo['b']+'\n'+foo['c']);

  7. #7
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (It's better than associating with objectionable arrays.)

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    There are such things as associative arrays in Javascript and the following is a perfectly valid way of defining one.

    Code:
    foo = [];
    foo['bar'] = 'bas';
    What Associative arrays in Javascript DON'T have is a length. You can determine the number of entries that are defined in an associative array using the following code:

    Code:
    var foo_length=0;
    for(var x in foo){
    foo_length++;
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall
    There are such things as associative arrays in Javascript and the following is a perfectly valid way of defining one.

    Code:
    foo = [];
    foo['bar'] = 'bas';
    What Associative arrays in Javascript DON'T have is a length. You can determine the number of entries that are defined in an associative array using the following code:

    Code:
    var foo_length=0;
    for(var x in foo){
    foo_length++;
    }
    Nope, as I said you are merely creating a new property (assuming it doesn't already exist) and giving it a value. For example

    Code:
    var foo = [];
    
    foo.push(1); 
    
    foo['push'] = 'bas';
    
    alert(foo); 
    
    foo.push(2); 
    
    alert(foo);
    will give you an error

    You might as well have used

    Code:
    var foo = new String();
    
    foo['bar'] = 'bas';
    
    alert(foo['bar']);

  10. #10
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript did not include arrays until version 1.1. The support of associative array syntax for addressing the properties of an object was considered sufficient to obviate the need for a strict implementation of associative arrays.

    To quote Flanagan: "Objects in JavaScript have a dual nature: an object can represent an unordered collection of named values or an ordered collection of numbered values. In the latter case, the object is called an array. . . . objects and arrays are fundamentally the same data type in JavaScript . . ."

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script type="text/javascript" language="JavaScript1.5">
    
    var myArray = ["me", 2, "you", "nobody", 6, "all together"], num = 0;
    
    function count(a) {
    
    if(a.toString().length > 0) num += 1;
    
    return num;
    
    }
    
    window.onload = function() {
    
    myArray.forEach(count);
    
    alert(num);
    
    }
    
    </script>
    cheers

  12. #12
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Shakespeare said "A rose by any other name would smell as sweet".

    If you can write Javascript code that looks like it is processing an associative array then the behind the scenes implementation doesn't matter - it is an associative array.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  13. #13
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, Uncle Will wrote: " O, be some other name! What's in a name? that which we call a rose By any other name would smell as sweet;" -- Romeo and Juliet, Act 2, Scene 2.

    Juliet was exhibiting wishful thinking. One can use associative array syntax to access objects in a page, for instance form elements. Are forms now associative arrays? Or is that wishful thinking, as well?

    Of course, then, there's King Henry VI: "For what hath broach'd this tumult but thy pride?"
    Last edited by JVLB; Oct 8, 2006 at 17:53.

  14. #14
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all your answers. It turned out that the Prototype framework was the offender by (unasked) adding all kinds of methods to existing Javascript Constructors so I can't easily loop through my little array anymore. But that's a whole other subject.


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
  •