Javascript Associative array syntax not valid. Why?!?!?!?

Discussion in 'Javascript' started by Kevin, May 11, 2004.

  1. Kevin

    Kevin Guest

    Help!

    Why are none of these valid?

    var arrayName = new Array();

    arrayName['key1']['key2'] = new Array('alpha_val', 1);
    arrayName['key1']['key2'] = ['alpha_val', 1];

    I'm creating/writing the array on the server side from Perl, but I
    keep getting a javascript error message saying
    "arrayName.key1 has no properties."

    I'm too bald to keep pulling my hair out... :)

    Appreciate any assistance.

    Thanks!

    K
    Kevin, May 11, 2004
    #1
    1. Advertising

  2. Kevin

    Lee Guest

    Kevin said:
    >
    >Help!
    >
    >Why are none of these valid?
    >
    >var arrayName = new Array();
    >
    >arrayName['key1']['key2'] = new Array('alpha_val', 1);
    >arrayName['key1']['key2'] = ['alpha_val', 1];
    >
    >I'm creating/writing the array on the server side from Perl, but I
    >keep getting a javascript error message saying
    >"arrayName.key1 has no properties."


    Arrays are one-dimensional.
    You created arrayName as an array, but you're also
    treating arrayName['key1'] as if it were an array,
    without having created it.

    var arrayName = new Array();
    arrayName['key1'] = new Array();
    arrayName['key1']['key2'] = new Array('alpha_val', 1);
    Lee, May 11, 2004
    #2
    1. Advertising

  3. Kevin

    Kevin Guest

    THanks for the help, Lee, However, now I'm getting a different,
    NONSENSICAL error:

    Here is my test code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>

    <head>
    <title></title>
    <meta name="GENERATOR" content="Quanta Plus">
    <script type="text/javascript" language="JavaScript1.3">
    var firstArray = new Array();
    var firstArray["key1"] = new Array();
    var firstArray["key1"]["key2"] = new Arary("A",1);
    var firstArray["key1"]["key3"] = new Array("b",2);

    function arrayWalk() {
    for (var i in firstArray) {
    for (var j in firstArray) {
    alert("The first key of firstArray, " + i + " and the 2nd key of firstArray, " + j + "give us the following values:\n\tFirst sub-value: " + firstArray[j][0] + "\n\t2nd sub-value: " + firstArray[j][1] + ".");
    }
    }
    }

    </script>
    </head>
    <body>
    <form action="#" name="formTest">
    <input type="button" value="Test Array" name="btnTest" onClick="arrayWalk()" />
    </form>

    </body>
    </html>

    The error says "Missing ; before the statement" and it's
    pointing to the space after the first double quote in line 9:

    var firstArray["key1"] = new Array();

    And before you ask, I tried single quotes, too, and got the same error... :)

    AAGGGGGGGGHHHHHHHH!!!!!!!!! :)



    On Mon, 10 May 2004 16:34:10 -0700, Lee wrote:

    > Kevin said:
    >>
    >>Help!
    >>
    >>Why are none of these valid?
    >>
    >>var arrayName = new Array();
    >>
    >>arrayName['key1']['key2'] = new Array('alpha_val', 1);
    >>arrayName['key1']['key2'] = ['alpha_val', 1];
    >>
    >>I'm creating/writing the array on the server side from Perl, but I
    >>keep getting a javascript error message saying
    >>"arrayName.key1 has no properties."

    >
    > Arrays are one-dimensional.
    > You created arrayName as an array, but you're also
    > treating arrayName['key1'] as if it were an array,
    > without having created it.
    >
    > var arrayName = new Array();
    > arrayName['key1'] = new Array();
    > arrayName['key1']['key2'] = new Array('alpha_val', 1);
    Kevin, May 11, 2004
    #3
  4. Kevin

    Kevin Guest

    Never mind, I finally figured it out...

    I only need the first "var"; after that, it's already declared...

    <sigh>...

    Thank you so much for your assistance!

    K

    On Mon, 10 May 2004 16:34:10 -0700, Lee wrote:

    > Kevin said:
    >>
    >>Help!
    >>
    >>Why are none of these valid?
    >>
    >>var arrayName = new Array();
    >>
    >>arrayName['key1']['key2'] = new Array('alpha_val', 1);
    >>arrayName['key1']['key2'] = ['alpha_val', 1];
    >>
    >>I'm creating/writing the array on the server side from Perl, but I
    >>keep getting a javascript error message saying
    >>"arrayName.key1 has no properties."

    >
    > Arrays are one-dimensional.
    > You created arrayName as an array, but you're also
    > treating arrayName['key1'] as if it were an array,
    > without having created it.
    >
    > var arrayName = new Array();
    > arrayName['key1'] = new Array();
    > arrayName['key1']['key2'] = new Array('alpha_val', 1);
    Kevin, May 11, 2004
    #4
  5. Kevin <> writes:

    > THanks for the help, Lee, However, now I'm getting a different,
    > NONSENSICAL error:


    The error message isn't exactly helpfull in finding the error, but
    the error is quite nonsensical too :)


    > <script type="text/javascript" language="JavaScript1.3">


    (why v1.3?)

    > var firstArray = new Array();


    Here you declare a variable and assign an array to it.

    > var firstArray["key1"] = new Array();


    The intial "var" here means "declare a variable", but what comes after
    is not an identifier. Drop the "var".

    As a side note, you don't need to use Arrays when you want an
    "associative array" (which is just another word for "map"). Pure
    objects are just as good, and doesn't inherit as much crap. So,
    you could change the declarations to:
    var firstArray = new Object();
    firstArray["key1"] = new Object();

    > var firstArray["key1"]["key2"] = new Arary("A",1);


    Again, drop the "var". Also, typo: ^^

    > var firstArray["key1"]["key3"] = new Array("b",2);

    And again.


    > The error says "Missing ; before the statement" and it's
    > pointing to the space after the first double quote in line 9:
    >
    > var firstArray["key1"] = new Array();


    I assume the missing ; would go after "firstArray" to make the
    declaration correct.

    > And before you ask, I tried single quotes, too, and got the same
    > error... :)


    Yes, they are completely equivalent in Javascript :)

    /L 'and please don't top post.'
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, May 11, 2004
    #5
  6. Kevin

    Grant Wagner Guest

    The other thing to consider is that:

    arrayName['key1'] isn't really a reference to an array element, it's a
    reference to the key1 property of "arrayName". Since you can't really
    use any of the other functionality of an Array (splice, push, join, etc)
    if you're using named (as opposed to numeric) "array indexes", you might
    as well use an Object:

    var objectName = new Object(); // or {};
    objectName['key1'] = {};
    objectName['key1']['key2'] = value;

    You can shorten the entire sequence above to:

    var objectName = { key1:{ key2:value } };

    Now it's clear that "key1" and "key2" are not "array indexes", they are
    properties of an object (which they are for the Array object as well,
    they're just masquerading as "array indexes").

    Kevin wrote:

    > Never mind, I finally figured it out...
    >
    > I only need the first "var"; after that, it's already declared...
    >
    > <sigh>...
    >
    > Thank you so much for your assistance!
    >
    > K
    >
    > On Mon, 10 May 2004 16:34:10 -0700, Lee wrote:
    >
    > > Kevin said:
    > >>
    > >>Help!
    > >>
    > >>Why are none of these valid?
    > >>
    > >>var arrayName = new Array();
    > >>
    > >>arrayName['key1']['key2'] = new Array('alpha_val', 1);
    > >>arrayName['key1']['key2'] = ['alpha_val', 1];
    > >>
    > >>I'm creating/writing the array on the server side from Perl, but I
    > >>keep getting a javascript error message saying
    > >>"arrayName.key1 has no properties."

    > >
    > > Arrays are one-dimensional.
    > > You created arrayName as an array, but you're also
    > > treating arrayName['key1'] as if it were an array,
    > > without having created it.
    > >
    > > var arrayName = new Array();
    > > arrayName['key1'] = new Array();
    > > arrayName['key1']['key2'] = new Array('alpha_val', 1);


    --
    | Grant Wagner <>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *
    http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-developer/upgrade_2.html
    Grant Wagner, May 14, 2004
    #6
  7. Kevin

    Evertjan. Guest

    My Pet Programmer wrote on 20 dec 2007 in comp.lang.javascript:

    > They don't have any properties. Multidimensional arrays in js don't
    > work like the good server side ones do (I'm php).


    Multidimensional arrays in javascript do not exist!

    You can define an array element a new array,
    but that does not influence other elements in their behavour !!!

    Javascript can be used clientside, serverside, and in a meriad of other
    ways. so the contradiction of serverside and javascript does not exist.

    You are not php, I met php several times,
    she did not look like you in the least.

    > You need to init each element of the second dimension before it can be
    > used. I usually build the second dim and tack in onto the first.


    A second dim in Javascript, what is that?

    > Matrices and cubes have actually given me quite a bit of trouble this
    > week.


    I am sorry to hear that.

    > var arrayName = new Array(); // first dim done


    > arrayName = 3; // valid
    > arrayName[j]; //invalid


    Yes, logically, my dear php:

    1 you just defined arrayName as having the numerical value of 3,
    and then you expect it to be an array in itself?

    2 arrays are not statements, usually.

    > Honestly, from the server side, I always use JSON formats. SO much
    > easier.


    "from the server side"?
    Oh yes, you profess to BE serverside.

    > Yours:


    Whose? Kevin's?

    [Please do not toppost on usenet]

    > arrayName =
    > [
    > {"key1" :
    > [ { "Key2" :
    > ["alpha_val":1]}
    > ]
    > ]
    >
    > ~A!
    > Kevin took the time to say:
    >> Help!
    >>
    >> Why are none of these valid?
    >>
    >> var arrayName = new Array();


    This is valid

    >> arrayName['key1']['key2'] = new Array('alpha_val', 1);


    You did not define the element to be an array!

    Even so arrays are keyed by numbers.

    You are using an object.

    Arrays are objects, and objects can have properties:

    look:

    <script type='text/javascript'>
    var a = []; // define an array;
    a['seven'] = 7;
    alert( a.length ); // the array is still empty of elements
    alert( a['seven'] ); // see, no hands mam! the object has a property
    </script>


    >> arrayName['key1']['key2'] = ['alpha_val', 1];


    I believe you cannot do that with an object,
    having a property have a property,
    unless the first property is defined as an object.

    <script type='text/javascript'>
    var a = {}; // define an object
    a['key1'] = {}; // define one of it's properties as an object
    a['key1']['key2'] = 8;
    alert(a['key1']['key2']); // see, no hands again, mam!
    </script>

    you could do this:

    <script type='text/javascript'>
    var a = []; // define an array-object
    a['key1'] = []; // define a property as an array-object
    a['key1']['key2'] = 8;
    alert(a['key1']['key2']);
    </script>

    but here the array was used as an object,
    the array behavour was not used at all.


    >> I'm creating/writing the array on the server side from Perl,


    With serverside javascript code, from perl? please explain.

    >> but I keep getting a javascript error message saying
    >> "arrayName.key1 has no properties."


    How do you know it was javascript,
    or do you get a clientside mesage?

    >> I'm too bald to keep pulling my hair out... :)


    Too ba[l]d.


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Dec 20, 2007
    #7
  8. Kevin

    Evertjan. Guest

    My Pet Programmer wrote on 21 dec 2007 in comp.lang.javascript:

    > And I don't think I did much of anything to deserve that tone from
    > you.


    [Please do not toppost on usenet]

    Dear php,

    I thought you(!) were asking about something.
    What tone would you think you "deserve"?
    Did my lenghty and tested examples help you?

    And even without any "deserving",
    this not being a paid callcenter,
    on usenet the asking cannot define the answer.

    > I said that they do not work the same as multi-dim arrays in
    > javascript, not that they don't work.


    What is "they"?
    This NG is about javascript.
    What would it matter if those "they" would not work,
    as "they" must be outside javascript.
    Or do you mean serverside javascript arrays?

    The concept of "dim" does not exist in javascript,
    [is that the Basic dim, what we call var, or short for dimension?]
    so I do not understand what you mean by a multi-dim array.

    > Evertjan. took the time to say:


    If you mean that, a 'thank you' could have been more proper.

    >> My Pet Programmer wrote on 20 dec 2007 in comp.lang.javascript:
    >>
    >>> They don't have any properties. Multidimensional arrays in js don't
    >>> work like the good server side ones do (I'm php).

    >>
    >> Multidimensional arrays in javascript do not exist!
    >>
    >> You can define an array element a new array,
    >> but that does not influence other elements in their behavour !!!
    >>
    >> Javascript can be used clientside, serverside, and in a meriad of
    >> other ways. so the contradiction of serverside and javascript does
    >> not exist.
    >>
    >> You are not php, I met php several times,
    >> she did not look like you in the least.
    >>
    >>> You need to init each element of the second dimension before it can
    >>> be used. I usually build the second dim and tack in onto the first.

    >>
    >> A second dim in Javascript, what is that?
    >>
    >>> Matrices and cubes have actually given me quite a bit of trouble
    >>> this week.

    >>
    >> I am sorry to hear that.
    >>
    >>> var arrayName = new Array(); // first dim done

    >>
    >>> arrayName = 3; // valid
    >>> arrayName[j]; //invalid

    >>
    >> Yes, logically, my dear php:
    >>
    >> 1 you just defined arrayName as having the numerical value of 3,
    >> and then you expect it to be an array in itself?
    >>
    >> 2 arrays are not statements, usually.
    >>
    >>> Honestly, from the server side, I always use JSON formats. SO much
    >>> easier.

    >>
    >> "from the server side"?
    >> Oh yes, you profess to BE serverside.
    >>
    >>> Yours:

    >>
    >> Whose? Kevin's?
    >>
    >> [Please do not toppost on usenet]
    >>
    >>> arrayName =
    >>> [
    >>> {"key1" :
    >>> [ { "Key2" :
    >>> ["alpha_val":1]}
    >>> ]
    >>> ]
    >>>
    >>> ~A!
    >>> Kevin took the time to say:
    >>>> Help!
    >>>>
    >>>> Why are none of these valid?
    >>>>
    >>>> var arrayName = new Array();

    >>
    >> This is valid
    >>
    >>>> arrayName['key1']['key2'] = new Array('alpha_val', 1);

    >>
    >> You did not define the element to be an array!
    >>
    >> Even so arrays are keyed by numbers.
    >>
    >> You are using an object.
    >>
    >> Arrays are objects, and objects can have properties:
    >>
    >> look:
    >>
    >> <script type='text/javascript'>
    >> var a = []; // define an array;
    >> a['seven'] = 7;
    >> alert( a.length ); // the array is still empty of elements
    >> alert( a['seven'] ); // see, no hands mam! the object has a
    >> property
    >> </script>
    >>
    >>
    >>>> arrayName['key1']['key2'] = ['alpha_val', 1];

    >>
    >> I believe you cannot do that with an object,
    >> having a property have a property,
    >> unless the first property is defined as an object.
    >>
    >> <script type='text/javascript'>
    >> var a = {}; // define an object
    >> a['key1'] = {}; // define one of it's properties as an object
    >> a['key1']['key2'] = 8;
    >> alert(a['key1']['key2']); // see, no hands again, mam!
    >> </script>
    >>
    >> you could do this:
    >>
    >> <script type='text/javascript'>
    >> var a = []; // define an array-object
    >> a['key1'] = []; // define a property as an array-object
    >> a['key1']['key2'] = 8;
    >> alert(a['key1']['key2']);
    >> </script>
    >>
    >> but here the array was used as an object,
    >> the array behavour was not used at all.
    >>
    >>
    >>>> I'm creating/writing the array on the server side from Perl,

    >>
    >> With serverside javascript code, from perl? please explain.
    >>
    >>>> but I keep getting a javascript error message saying
    >>>> "arrayName.key1 has no properties."

    >>
    >> How do you know it was javascript,
    >> or do you get a clientside mesage?
    >>
    >>>> I'm too bald to keep pulling my hair out... :)

    >>
    >> Too ba[l]d.
    >>
    >>

    >




    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Dec 21, 2007
    #8
  9. Kevin

    Evertjan. Guest

    My Pet Programmer wrote on 21 dec 2007 in comp.lang.javascript:

    > > The concept of "dim" does not exist in javascript,
    > > [is that the Basic dim, what we call var, or short for dimension?]
    > > so I do not understand what you mean by a multi-dim array.

    >
    > PS - I like this part the best. We're talking about JavaScript
    > multidimensional arrays, and you think somehow we're talking about VB.
    > Love it. The concept of "very, very dim" obviously exists somewhere.


    I love talking about nonexisting things too, but,
    as JavaScript multidimensional arrays do NOT exist,
    this NG is not the place for such a dim view.

    For a moment I contemplated about a hidden browser window
    on a Dim Sum smoke screen.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Dec 21, 2007
    #9
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,806
    Smokey Grindel
    Dec 2, 2006
  2. desktop
    Replies:
    5
    Views:
    383
    James Kanze
    Jun 26, 2007
  3. Yvon Thoraval
    Replies:
    5
    Views:
    205
    Jason Creighton
    Sep 17, 2003
  4. VK
    Replies:
    47
    Views:
    536
    Thomas 'PointedEars' Lahn
    Jul 13, 2005
  5. VK
    Replies:
    36
    Views:
    641
    Martin Honnen
    Aug 3, 2005
Loading...

Share This Page