Array problem: has no properties - why?

R

Randell D.

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

Janwillem Borleffs

Randell said:
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
 
R

Richard

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

Douglas Crockford

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
 
R

Richard Cornford

Richard said:
I have a multi-dimensional array in javascript, as follows:
gameRecord=new Array(500);
gameRecord[1][1]="FA Cup : 19 February 2005";
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[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.
 
R

Richard

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.

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

Michael Winter

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
 
R

Randell D.

Douglas said:
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.
 
R

Randell D.

Michael said:
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.
 
M

Michael Winter

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
 
D

Douglas Crockford

I'm glad to hear about the 'zero element' because my array did
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
 
D

Dr John Stockton

JRS: In article <[email protected]
m>, dated Sun, 20 Feb 2005 08:04:58, seen in
Douglas Crockford said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top