473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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]="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
11 5555
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
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
> 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
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
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
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
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
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
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
>> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Alexis Nikichine | last post by:
Hello, It is common knowledge that arrays can be used as hashtables: var color = ; color = 0xFF0000; color = 0x0000FF;
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
5
by: Jon Maz | last post by:
Hi All, I'm reasonably proficient with C#, and am starting with php5. I was happy to learn of the __get and __set methods, but am encountering a problem using them to set an object property of...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
9
by: IamIan | last post by:
I'm using an array to store map features (name, lat, lon, caption, etc), from which the user can then select an individual feature. The problem is that when thousands of features are stored in the...
14
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that...
5
by: Pukeko | last post by:
Hi, this is an array that is used for a dropdown menu. var imageArray = new Array( "ecwp://" + document.location.host + "/massey/images/massey/ massey_07_fullres.ecw", "ecwp://" +...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.