By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Array problem: has no properties - why?

P: n/a

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.
Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a
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.
Jul 23 '05 #3

P: n/a
> 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
Jul 23 '05 #4

P: n/a
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.
Jul 23 '05 #5

P: n/a
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.
Jul 23 '05 #6

P: n/a
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.
Jul 23 '05 #7

P: n/a
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.
Jul 23 '05 #8

P: n/a
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.
Jul 23 '05 #9

P: n/a
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.
Jul 23 '05 #10

P: n/a
>> 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
Jul 23 '05 #11

P: n/a
JRS: In article <59***************************@msgid.meganewsserve rs.co
m>, dated Sun, 20 Feb 2005 08:04:58, seen in news:comp.lang.javascript,
Douglas Crockford <no****@covad.net> 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.
Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.