Array problem: has no properties - why?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randell D.

    #1

    Array problem: has no properties - why?


    Folks

    I have a multi-dimensional array in javascript, as follows:

    gameRecord=new Array(500);
    gameRecord[1][1]="FA Cup : 19 February 2005";
    gameRecord[1][2][1]="Arsenal v Sheff Utd, 12:30";
    gameRecord[1][2][2]="Bolton v Fulham, 15:00";
    gameRecord[1][2][3]="Charlton v Leicester, 15:00";
    gameRecord[1][2][4]="Everton v Man Utd, 17:30";
    gameRecord[1][2][5]="Southampto n v Brentford, 15:00";

    There are alot more records to it, but the above is just an extract...
    It is written into its own javascript file (called football.js) which I
    call using

    <script language="JavaS cript" type="text/javascript"
    src="football.j s"></script>

    I know its reading it because for a test, I preceeded the file with a
    simple alert("here"); and this shouted back at me.

    How come though that even before I read from the array I get the
    following error message in the JavaScript Console in Mozilla...

    Error: gameRecord[1] has no properties

    I gather I am initializing the array incorrectly - would someone care to
    direct me in the right direction?

    Thanks
    Randell D.
  • Janwillem Borleffs

    #2
    Re: Array problem: has no properties - why?

    Randell D. wrote:[color=blue]
    > I gather I am initializing the array incorrectly - would someone care
    > to direct me in the right direction?
    >[/color]

    You should initialize arrays before using them, including multidimensiona l
    arrays:

    var gameRecord = [];
    gameRecord[1] = [];
    gameRecord[1][1] = "FA Cup : 19 February 2005";
    gameRecord[1][2] = [];
    gameRecord[1][2][1] = "Arsenal v Sheff Utd, 12:30";


    JW


    Comment

    • Richard

      #3
      Re: Array problem: has no properties - why?

      On Sat, 19 Feb 2005 09:30:05 GMT Randell D. wrote:

      [color=blue]
      > Folks[/color]
      [color=blue]
      > I have a multi-dimensional array in javascript, as follows:[/color]
      [color=blue]
      > gameRecord=new Array(500);
      > gameRecord[1][1]="FA Cup : 19 February 2005";
      > gameRecord[1][2][1]="Arsenal v Sheff Utd, 12:30";
      > gameRecord[1][2][2]="Bolton v Fulham, 15:00";
      > gameRecord[1][2][3]="Charlton v Leicester, 15:00";
      > gameRecord[1][2][4]="Everton v Man Utd, 17:30";
      > gameRecord[1][2][5]="Southampto n v Brentford, 15:00";[/color]
      [color=blue]
      > There are alot more records to it, but the above is just an extract...
      > It is written into its own javascript file (called football.js) which I
      > call using[/color]
      [color=blue]
      > <script language="JavaS cript" type="text/javascript"
      > src="football.j s"></script>[/color]
      [color=blue]
      > I know its reading it because for a test, I preceeded the file with a
      > simple alert("here"); and this shouted back at me.[/color]
      [color=blue]
      > How come though that even before I read from the array I get the
      > following error message in the JavaScript Console in Mozilla...[/color]
      [color=blue]
      > Error: gameRecord[1] has no properties[/color]
      [color=blue]
      > I gather I am initializing the array incorrectly - would someone care
      > to
      > direct me in the right direction?[/color]
      [color=blue]
      > Thanks
      > Randell D.[/color]


      gameRecord[1]="Top_Level" .
      Since you identified it, the script expects something to be in it.
      Nothing says you have to use it.


      Comment

      • Douglas Crockford

        #4
        Re: Array problem: has no properties - why?

        > I have a multi-dimensional array in javascript, as follows:[color=blue]
        >
        > gameRecord=new Array(500);
        > gameRecord[1][1]="FA Cup : 19 February 2005";
        > gameRecord[1][2][1]="Arsenal v Sheff Utd, 12:30";
        > gameRecord[1][2][2]="Bolton v Fulham, 15:00";
        > gameRecord[1][2][3]="Charlton v Leicester, 15:00";
        > gameRecord[1][2][4]="Everton v Man Utd, 17:30";
        > gameRecord[1][2][5]="Southampto n v Brentford, 15:00";
        >
        > There are alot more records to it, but the above is just an extract...
        > It is written into its own javascript file (called football.js) which I
        > call using
        >
        > <script language="JavaS cript" type="text/javascript"
        > src="football.j s"></script>
        >
        > I know its reading it because for a test, I preceeded the file with a
        > simple alert("here"); and this shouted back at me.
        >
        > How come though that even before I read from the array I get the
        > following error message in the JavaScript Console in Mozilla...
        >
        > Error: gameRecord[1] has no properties
        >
        > I gather I am initializing the array incorrectly - would someone care to
        > direct me in the right direction?[/color]

        gameRecord = [
        ["FA Cup : 19 February 2005", [
        "Arsenal v Sheff Utd, 12:30",
        "Bolton v Fulham, 15:00",
        "Charlton v Leicester, 15:00",
        "Everton v Man Utd, 17:30",
        "Southampto n v Brentford, 15:00"]]];

        In this form, it is easier to read, it is easier to modify, and it is
        correct. Learn to use the literal notation for arrays and objects.

        Also, array subscripts should begin at 0, not 1.


        Comment

        • Richard Cornford

          #5
          Re: Array problem: has no properties - why?

          Richard wrote:[color=blue]
          > On Sat, 19 Feb 2005 09:30:05 GMT Randell D. wrote:[color=green]
          >> I have a multi-dimensional array in javascript, as follows:[/color]
          >[color=green]
          >> gameRecord=new Array(500);
          >> gameRecord[1][1]="FA Cup : 19 February 2005";[/color][/color]
          <snip>[color=blue][color=green]
          >> Error: gameRecord[1] has no properties[/color]
          >[color=green]
          >> I gather I am initializing the array incorrectly -
          >> would someone care to direct me in the right direction?[/color][/color]
          <snip>[color=blue]
          > gameRecord[1]="Top_Level" .
          > Since you identified it, the script expects something
          > to be in it. Nothing says you have to use it.[/color]

          Why do you do this? Even if you cannot see it for yourself you have been
          told often enough that you don't know anything about scripting web
          browsers. Posting your mystical incantations in response to serious
          questions about programming is going to do nobody any good, and make you
          look like more of a fool than is already apparent from your established
          record on Usenet.

          Richard.


          Comment

          • Richard

            #6
            Re: Array problem: has no properties - why?

            On Sat, 19 Feb 2005 08:13:59 -0800 Douglas Crockford wrote:
            [color=blue][color=green]
            >> I have a multi-dimensional array in javascript, as follows:[/color][/color]
            [color=blue][color=green]
            >> gameRecord=new Array(500);
            >> gameRecord[1][1]="FA Cup : 19 February 2005";
            >> gameRecord[1][2][1]="Arsenal v Sheff Utd, 12:30";
            >> gameRecord[1][2][2]="Bolton v Fulham, 15:00";
            >> gameRecord[1][2][3]="Charlton v Leicester, 15:00";
            >> gameRecord[1][2][4]="Everton v Man Utd, 17:30";
            >> gameRecord[1][2][5]="Southampto n v Brentford, 15:00";[/color][/color]
            [color=blue][color=green]
            >> There are alot more records to it, but the above is just an extract...
            >> It is written into its own javascript file (called football.js) which
            >> I
            >> call using[/color][/color]
            [color=blue][color=green]
            >> <script language="JavaS cript" type="text/javascript"
            >> src="football.j s"></script>[/color][/color]
            [color=blue][color=green]
            >> I know its reading it because for a test, I preceeded the file with a
            >> simple alert("here"); and this shouted back at me.[/color][/color]
            [color=blue][color=green]
            >> How come though that even before I read from the array I get the
            >> following error message in the JavaScript Console in Mozilla...[/color][/color]
            [color=blue][color=green]
            >> Error: gameRecord[1] has no properties[/color][/color]
            [color=blue][color=green]
            >> I gather I am initializing the array incorrectly - would someone care
            >> to
            >> direct me in the right direction?[/color][/color]
            [color=blue]
            > gameRecord = [
            > ["FA Cup : 19 February 2005", [
            > "Arsenal v Sheff Utd, 12:30",
            > "Bolton v Fulham, 15:00",
            > "Charlton v Leicester, 15:00",
            > "Everton v Man Utd, 17:30",
            > "Southampto n v Brentford, 15:00"]]];[/color]
            [color=blue]
            > In this form, it is easier to read, it is easier to modify, and it is
            > correct. Learn to use the literal notation for arrays and objects.[/color]
            [color=blue]
            > Also, array subscripts should begin at 0, not 1.[/color]
            [color=blue]
            > http://www.JSON.org[/color]

            Subscript arrays always begin at zero.
            It is your choice to use it or not to use it.
            Besides, he was merely showing an example.


            Comment

            • Michael Winter

              #7
              Re: Array problem: has no properties - why?

              Richard wrote:

              [snip]

              Douglas was responding to the OP. You were not. Trim unnecessary
              quoted material when posting. You read enough technical newsgroups to
              know what is expected of you.
              [color=blue]
              > Subscript arrays [...][/color]

              What's a "subscript array"?
              [color=blue]
              > It is your choice to use it or not to use it.[/color]

              It is up the author to decide what to use, yes. However, it may not
              have been the intention here to create a sparse array. Indeed, what if
              the author uses the length property without realising that it will
              reflect the "surplus" element.

              Unless the OP is omitting some crucial data, I see no benefit here in
              skipping element zero.
              [color=blue]
              > Besides, he was merely showing an example.[/color]

              If the actual code used element zero, why wouldn't this example? In
              any case, if the example or the OP doesn't provide enough information
              to receive accurate replies, that's the fault of the OP.

              Mike

              --
              Michael Winter
              Replace ".invalid" with ".uk" to reply by e-mail.

              Comment

              • Randell D.

                #8
                Re: Array problem: has no properties - why?

                Douglas Crockford wrote:
                [color=blue][color=green]
                >> I have a multi-dimensional array in javascript, as follows:
                >>
                >> gameRecord=new Array(500);
                >> gameRecord[1][1]="FA Cup : 19 February 2005";
                >> gameRecord[1][2][1]="Arsenal v Sheff Utd, 12:30";
                >> gameRecord[1][2][2]="Bolton v Fulham, 15:00";
                >> gameRecord[1][2][3]="Charlton v Leicester, 15:00";
                >> gameRecord[1][2][4]="Everton v Man Utd, 17:30";
                >> gameRecord[1][2][5]="Southampto n v Brentford, 15:00";
                >>
                >> There are alot more records to it, but the above is just an extract...
                >> It is written into its own javascript file (called football.js) which
                >> I call using
                >>
                >> <script language="JavaS cript" type="text/javascript"
                >> src="football.j s"></script>
                >>
                >> I know its reading it because for a test, I preceeded the file with a
                >> simple alert("here"); and this shouted back at me.
                >>
                >> How come though that even before I read from the array I get the
                >> following error message in the JavaScript Console in Mozilla...
                >>
                >> Error: gameRecord[1] has no properties
                >>
                >> I gather I am initializing the array incorrectly - would someone care
                >> to direct me in the right direction?[/color]
                >
                >
                > gameRecord = [
                > ["FA Cup : 19 February 2005", [
                > "Arsenal v Sheff Utd, 12:30",
                > "Bolton v Fulham, 15:00",
                > "Charlton v Leicester, 15:00",
                > "Everton v Man Utd, 17:30",
                > "Southampto n v Brentford, 15:00"]]];
                >
                > In this form, it is easier to read, it is easier to modify, and it is
                > correct. Learn to use the literal notation for arrays and objects.
                >
                > Also, array subscripts should begin at 0, not 1.
                >
                > http://www.JSON.org[/color]

                Thanks for the note above, however the above was only a short extract of
                a few hundred records and doing it that way could prove cumbersome,
                especially in balancing the square brackets.

                I've solved the problem though and put it down to not declarying the
                multidimension array properly... The Javascript Console in Mozilla no
                longer barks back at me...

                Thanks for letting me know that arrays should begin with 0 and not 1.

                Cheers
                Randell D.

                Comment

                • Randell D.

                  #9
                  Re: Array problem: has no properties - why?

                  Michael Winter wrote:
                  [color=blue]
                  > Richard wrote:
                  >
                  > [snip]
                  >
                  > Douglas was responding to the OP. You were not. Trim unnecessary quoted
                  > material when posting. You read enough technical newsgroups to know what
                  > is expected of you.
                  >[color=green]
                  >> Subscript arrays [...][/color]
                  >
                  >
                  > What's a "subscript array"?
                  >[color=green]
                  >> It is your choice to use it or not to use it.[/color]
                  >
                  >
                  > It is up the author to decide what to use, yes. However, it may not have
                  > been the intention here to create a sparse array. Indeed, what if the
                  > author uses the length property without realising that it will reflect
                  > the "surplus" element.
                  >
                  > Unless the OP is omitting some crucial data, I see no benefit here in
                  > skipping element zero.
                  >[color=green]
                  >> Besides, he was merely showing an example.[/color]
                  >
                  >
                  > If the actual code used element zero, why wouldn't this example? In any
                  > case, if the example or the OP doesn't provide enough information to
                  > receive accurate replies, that's the fault of the OP.
                  >
                  > Mike
                  >[/color]

                  What have I started?

                  I'm glad to hear about the 'zero element' because my array did
                  (incorrectly) begin with 1, and not zero. In addition, you've helped
                  correct me in such that I originally believed that js having three
                  elements numbered (for example) 3, 5 and 9 would give a length of three,
                  and not nine. I've since references a Core Guide to Javascript that I
                  downloaded sometime ago from Netscape which supports your arguement, and
                  throws my previous understanding out the window.

                  Thanks again for everyone's help.

                  Randell D.

                  Comment

                  • Michael Winter

                    #10
                    Re: Array problem: has no properties - why?

                    Randell D. wrote:

                    [snip]
                    [color=blue]
                    > What have I started?[/color]

                    Nothing. It's just RtS being his normal self. Ignore him. :)
                    [color=blue]
                    > I'm glad to hear about the 'zero element' because my array did
                    > (incorrectly) begin with 1, and not zero.[/color]

                    Well, as RtS said, you can define entries at any index you like.
                    However, you must remember that the elements you skip still exist (to
                    an extent). The length property is updated to reflect the highest
                    index plus one. So assigning values to elements 3, 5, 9 will result in
                    a length of ten (*not* nine, as you thought). The other seven elements
                    will remain undefined.

                    [snip]

                    Mike

                    --
                    Michael Winter
                    Replace ".invalid" with ".uk" to reply by e-mail.

                    Comment

                    • Douglas Crockford

                      #11
                      Re: Array problem: has no properties - why?

                      >> I'm glad to hear about the 'zero element' because my array did[color=blue][color=green]
                      >> (incorrectly) begin with 1, and not zero.[/color]
                      >
                      >
                      > Well, as RtS said, you can define entries at any index you like.
                      > However, you must remember that the elements you skip still exist (to an
                      > extent). The length property is updated to reflect the highest index
                      > plus one. So assigning values to elements 3, 5, 9 will result in a
                      > length of ten (*not* nine, as you thought). The other seven elements
                      > will remain undefined.[/color]

                      In JavaScript arrays, the numeric indexes are converted to strings, so

                      my_array[0]

                      and

                      my_array['0']

                      reach the same element. It is not like a traditional array that has a
                      linear sequence of numbered slots. You can think of JavaScript arrays as
                      being sparce arrays: only slots with values consume memory.

                      The value of a missing element is undefined.


                      Comment

                      • Dr John Stockton

                        #12
                        Re: Array problem: has no properties - why?

                        JRS: In article <59dd9$4218b547 $44a4ae49$28753 @msgid.meganews servers.co
                        m>, dated Sun, 20 Feb 2005 08:04:58, seen in news:comp.lang. javascript,
                        Douglas Crockford <nospam@covad.n et> posted :[color=blue]
                        >
                        >In JavaScript arrays,[/color]
                        [color=blue]
                        >The value of a missing element is undefined.[/color]

                        That's either ambiguous or wrong, considering it on its own; and,
                        considering its author, it cannot be wrong.

                        The value of a missing element is defined, and it is defined as the
                        special value called undefined. That value itself is well-defined,
                        although its internal representation probably is not.

                        [][0] == [][1] evaluates, by definition AIUI, as the value true.


                        In discussing javascript, undefined should not be used to mean not-
                        defined; but, lest it be so misinterpreted, neither should it be used
                        unaided to mean what it rightly means.

                        --
                        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                        <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                        Comment

                        Working...