quick question about labelling array elements

Discussion in 'Javascript' started by Ciaran, Mar 27, 2007.

  1. Ciaran

    Ciaran Guest

    Im a php coder - not really used to javascript. Can someone please
    tell me how to assign labels to array elements and then call them by
    the index?

    var mycars = new Array(3)
    mycars['bigone'] = "Saab"
    mycars['biggerone'] = "Volvo"
    mycars[2] = "BMW"

    document.write(mycars[0]);

    is printing nothing when in php the same idea would spit out "Saab"

    Cheers,
    Ciarán
     
    Ciaran, Mar 27, 2007
    #1
    1. Advertising

  2. On Mar 26, 7:09 pm, "Ciaran" <> wrote:
    > Im a php coder - not really used to javascript. Can someone please
    > tell me how to assign labels to array elements


    use object

    mycars={}
    mycars['bigone']="Saab"
    ....

    > and then call them by
    > the index?


    not possible with object
     
    scripts.contact, Mar 27, 2007
    #2
    1. Advertising

  3. Ciaran

    Ciaran Guest

    On Mar 27, 2:09 am, "Ciaran" <> wrote:
    > Im a php coder - not really used to javascript. Can someone please
    > tell me how to assign labels to array elements and then call them by
    > the index?
    >
    > var mycars = new Array(3)
    > mycars['bigone'] = "Saab"
    > mycars['biggerone'] = "Volvo"
    > mycars[2] = "BMW"
    >
    > document.write(mycars[0]);
    >
    > is printing nothing when in php the same idea would spit out "Saab"


    Never Mind I found a solution:
    for (x in mycars)
    {
    document.write(mycars[x])
    }

    Thanks anyway guys!
    Ciarán
     
    Ciaran, Mar 27, 2007
    #3
  4. Ciaran

    -Lost Guest

    > "Ciaran" <> wrote in message
    > news:...
    > Im a php coder - not really used to javascript. Can someone please
    > tell me how to assign labels to array elements and then call them by
    > the index?


    > var mycars = new Array(3)
    > mycars['bigone'] = "Saab"
    > mycars['biggerone'] = "Volvo"
    > mycars[2] = "BMW"


    > document.write(mycars[0]);


    > is printing nothing when in php the same idea would spit out "Saab"


    That is because your first declare it as an "associative" array, which, whilst it does not
    exist in JavaScript, is a way to access the values. I may be wording this incorrectly, so
    I will give an example.

    arr1['author'] = 'Stephen King';
    arr1.push('0th');

    Now, if you do:

    alert(arr1[0]);

    .... you get 0th. Only arr1['author'] would retrieve what you expect.

    The best method of doing what you want (entirely associative) is to use JSON on an Object.

    var parents = {};
    parents = {
    'daddy' : 'The Dad',
    'mommy' : 'The Mom'
    }

    This actually led me to write a routine that took an indice and a value and wrote to the
    array accordingly (thereby giving me what you know as an associative array).

    -Lost
     
    -Lost, Mar 27, 2007
    #4
  5. Ciaran

    -Lost Guest

    >> "Ciaran" <> wrote in message
    >> news:...

    On Mar 27, 2:09 am, "Ciaran" <> wrote:
    >> Im a php coder - not really used to javascript. Can someone please
    >> tell me how to assign labels to array elements and then call them by
    >> the index?
    >>
    >> var mycars = new Array(3)
    >> mycars['bigone'] = "Saab"
    >> mycars['biggerone'] = "Volvo"
    >> mycars[2] = "BMW"
    >>
    >> document.write(mycars[0]);
    >>
    >> is printing nothing when in php the same idea would spit out "Saab"


    >Never Mind I found a solution:
    >for (x in mycars)
    >{
    >document.write(mycars[x])
    >}


    Alright, even though, baring in mind, that is *not* what you asked for. If you had said
    how to I retrieve all of my elements in the array, I would have given you the for-in loop.

    > Thanks anyway guys!


    You are most welcome.

    -Lost
     
    -Lost, Mar 27, 2007
    #5
  6. Ciaran

    Ciaran Guest

    On Mar 27, 2:55 am, "-Lost" <> wrote:

    > var parents = {};
    > parents = {
    > 'daddy' : 'The Dad',
    > 'mommy' : 'The Mom'
    >
    > }


    Thanks a lot Lost, This explains a lot and is much clearer than the
    average posts on this topic. Is there a way to call the 'mommy' using
    an index by chance?

    Ciarán
     
    Ciaran, Mar 27, 2007
    #6
  7. Ciaran

    Lee Guest

    Ciaran said:
    >
    >On Mar 27, 2:55 am, "-Lost" <> wrote:
    >
    >> var parents =3D {};
    >> parents =3D {
    >> 'daddy' : 'The Dad',
    >> 'mommy' : 'The Mom'
    >>
    >> }

    >
    >Thanks a lot Lost, This explains a lot and is much clearer than the
    >average posts on this topic. Is there a way to call the 'mommy' using
    >an index by chance?


    No, because parents['mommy'] is not actually an element of the array.
    It is a new attribute that has been added to the Array object.

    "square bracket" notation has two completely different meanings,
    depending on whether the brackets contain an integer or a string.
    They both look like they access array elements, but that's not
    really what's going on:

    parents[integer] sets or gets the array element at position integer
    parents[string] sets or gets the object attribute named by string


    --
     
    Lee, Mar 27, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cmtibmFpcg==?=

    Pie Chart - Labelling

    =?Utf-8?B?cmtibmFpcg==?=, Jan 21, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    534
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Jan 21, 2005
  2. JKop
    Replies:
    11
    Views:
    934
  3. Greg G
    Replies:
    2
    Views:
    2,033
    Greg G
    Jul 12, 2006
  4. P
    Replies:
    1
    Views:
    1,201
    Joe Kesselman
    Jul 7, 2006
  5. Bit Byter
    Replies:
    2
    Views:
    626
    Barry Schwarz
    Apr 16, 2006
Loading...

Share This Page