Array problem: has no properties - why?

Discussion in 'Javascript' started by Randell D., Feb 19, 2005.

  1. Randell D.

    Randell D. Guest

    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]="Southampton 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="JavaScript" type="text/javascript"
    src="football.js"></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.
    Randell D., Feb 19, 2005
    #1
    1. Advertising

  2. Randell D. wrote:
    > I gather I am initializing the array incorrectly - would someone care
    > to direct me in the right direction?
    >


    You should initialize arrays before using them, including multidimensional
    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
    Janwillem Borleffs, Feb 19, 2005
    #2
    1. Advertising

  3. Randell D.

    Richard Guest

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


    > 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]="Southampton 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="JavaScript" type="text/javascript"
    > src="football.js"></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.



    gameRecord[1]="Top_Level".
    Since you identified it, the script expects something to be in it.
    Nothing says you have to use it.
    Richard, Feb 19, 2005
    #3
  4. > 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]="Southampton 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="JavaScript" type="text/javascript"
    > src="football.js"></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?


    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",
    "Southampton 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
    Douglas Crockford, Feb 19, 2005
    #4
  5. Richard wrote:
    > On Sat, 19 Feb 2005 09:30:05 GMT Randell D. wrote:
    >> I have a multi-dimensional array in javascript, as follows:

    >
    >> gameRecord=new Array(500);
    >> gameRecord[1][1]="FA Cup : 19 February 2005";

    <snip>
    >> Error: gameRecord[1] has no properties

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

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


    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.
    Richard Cornford, Feb 19, 2005
    #5
  6. Randell D.

    Richard Guest

    On Sat, 19 Feb 2005 08:13:59 -0800 Douglas Crockford wrote:

    >> 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]="Southampton 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="JavaScript" type="text/javascript"
    >> src="football.js"></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?


    > 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",
    > "Southampton 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


    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.
    Richard, Feb 19, 2005
    #6
  7. 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.

    > Subscript arrays [...]


    What's a "subscript array"?

    > It is your choice to use it or not to use it.


    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.

    > Besides, he was merely showing an example.


    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.
    Michael Winter, Feb 20, 2005
    #7
  8. Randell D.

    Randell D. Guest

    Douglas Crockford wrote:

    >> 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]="Southampton 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="JavaScript" type="text/javascript"
    >> src="football.js"></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?

    >
    >
    > 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",
    > "Southampton 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


    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.
    Randell D., Feb 20, 2005
    #8
  9. Randell D.

    Randell D. Guest

    Michael Winter wrote:

    > 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.
    >
    >> Subscript arrays [...]

    >
    >
    > What's a "subscript array"?
    >
    >> It is your choice to use it or not to use it.

    >
    >
    > 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.
    >
    >> Besides, he was merely showing an example.

    >
    >
    > 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
    >


    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.
    Randell D., Feb 20, 2005
    #9
  10. Randell D. wrote:

    [snip]

    > What have I started?


    Nothing. It's just RtS being his normal self. Ignore him. :)

    > I'm glad to hear about the 'zero element' because my array did
    > (incorrectly) begin with 1, and not zero.


    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.
    Michael Winter, Feb 20, 2005
    #10
  11. >> I'm glad to hear about the 'zero element' because my array did
    >> (incorrectly) begin with 1, and not zero.

    >
    >
    > 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.


    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.

    http://www.crockford.com/javascript/survey.html
    Douglas Crockford, Feb 20, 2005
    #11
  12. JRS: In article <59dd9$4218b547$44a4ae49$
    m>, dated Sun, 20 Feb 2005 08:04:58, seen in news:comp.lang.javascript,
    Douglas Crockford <> posted :
    >
    >In JavaScript arrays,


    >The value of a missing element is undefined.


    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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Feb 20, 2005
    #12
    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

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,766
    Smokey Grindel
    Dec 2, 2006
  3. RC
    Replies:
    1
    Views:
    1,299
    Martin Honnen
    May 31, 2007
  4. laredotornado
    Replies:
    1
    Views:
    1,311
    markspace
    Dec 29, 2009
  5. RC
    Replies:
    1
    Views:
    84
    Martin Honnen
    May 31, 2007
Loading...

Share This Page