Click to See Complete Forum and Search --> : Array size. Help please?
econnections
07-28-2005, 11:16 AM
Hi,
I want to use an array to contain the data that is written to the rows of a table.
Each row represent the time of an appointment slot, ie 0800, 0830, 0900,
and the <td> is identified by id=1, id=2 etc.Thus using switch I can find out that to write the 1000 appointment details in the table I would use id=5 and
document.getElementById(5).innerHTML=appointmentDetails;
What I would like to know is if I do away with switch and label the id's with the appointment time, will this cause the array to waste memory. ie
if I have an 1830 appointment then
appointment[1830]='Performance review (Bristol office)';
document.getElementById(5).innerHTML=appointment[1830]
Any advice would be must appreciated.
Graham Uk
Mongus
07-28-2005, 11:28 AM
I would use string indexes instead of numeric indexes.
appointment['1830']='Performance review (Bristol office)';
You won't have to worry about tons of empty elements.
Jeff Mott
07-28-2005, 02:44 PM
use string indexes instead of numeric indexes ... You won't have to worry about tons of empty elementsEither way would actually be just fine. Arrays in JavaScript are essentially Objects with a length property. Which means that a JS array is not actually ordered, contiguous memory; it is a hash map.
Mongus
07-28-2005, 02:49 PM
I thought that might be the case but wasn't sure.
Thanks for clarifying!
econnections
07-28-2005, 05:04 PM
Using string indexes is new to me so I thought I would experiment with the function below. I can't get it to work, can anyone see a fault?
function getdata() {
var ttt new Array();
ttt['y'] = 'joe smith';
document.write(ttt['y']);
}
Thanks
Mongus
07-28-2005, 05:32 PM
Missing an equals sign in your ttt assignment.
var ttt = new Array();
econnections
07-28-2005, 05:44 PM
Now why couldn't I see that? I've been looking at the code for 30-minutes and it just wasn't obvious!
Thats so much.
Graham UK
i would have used 2nd range arrays instead of string indexing... Why not
var ttt = new Array();
ttt[0] = ['joe smith',1830];
ttt[1] = ['mary lou,1900];
now you have the desired correspondence, if I well undesrtood. The index might be the cell's index, and the name and hour are to be easily found as
var thename = ttt[cellindex][0]
var thetime = ttt[cellindex][1]
econnections
07-29-2005, 07:58 PM
This is really interesting and I think will do the job. I've not heard of '2nd range arrays' and putting string content into [...]
I've been looking through my Javascript books and search the internet for further information on using arrays in this way but have not found anything. Please advise of where there maybe some information or turorials on this.
Many thanks
Graham
Mongus
07-29-2005, 09:50 PM
Search for 2 dimensional arrays or multi-dimensional arrays. Basically they are just arrays with arrays for their elements. That is common to every programming language I'm aware of.
Searching for associative arrays will get you more information on using strings for indexes.