PDA

View Full Version : Problem accessing items in a 2D array...plz help


shivboy
11-23-2002, 08:00 AM
hi,
im continuing with my experimentation with 2D arrays in javascript. i simply created 2 rows of 20 layes each. then i created a 2D array of layer id's. now i wanna access a particular set layer id s which im unable to. can anyone plz help me with this? im posting the code below. any help is appreciated.



var oTh = new Array();
for (var i = 1; i <= 2; i++)
{
oTh[i] = new Array();
{
for (var j = 1; j <= 20; j++)
{
eval('oTh["'+i+'"]["'+j+'"]= "div'+i+'_'+j+'"');
}
}
}
for(var i = 1; k <=2; i++)
{
for(j = 1; j<=20; j++)
{
var m = oTh[k][j];
m.style.backgroundColor = "red"; }
}


but it generates an error at the bold line that oTh is undefined, why?

beetle
11-23-2002, 08:10 AM
Not sure, but tell me this...what are these doing? :Dvar oTh = new Array();
for (var i = 1; i <= 2; i++)
{
oTh[i] = new Array();
{ <---
for (var j = 1; j <= 20; j++)
{
eval('oTh["'+i+'"]["'+j+'"]= "div'+i+'_'+j+'"');
}
} <---
}
for(var i = 1; k <=2; i++)
{
for(j = 1; j<=20; j++)
{
var m = oTh[k][j];
m.style.backgroundColor = "red"; }
}

solal
11-24-2002, 03:27 PM
the one who answered you doesn't understand!

it's correct the form, because example in pascal u do:

for i:=1 to 2 do
begin
for j:=1 to 20 to
..........
..........
end;

your problem is that:

you wrote oTh [k][j];
and you r variables are i,j

so the correrction is
m = oTh [i][j]....... and the rest of your statement

Adam20002
11-24-2002, 10:49 PM
solal

we aren't working in pascal though ! The curly braces beetle pointed out appear to be doing nothing and should be removed.

in javascript you may use curly braces to group multiple statements in a loop of if statement for example which is what you seem to be getting in your post about Pascal syntax.

vibes

beetle
11-24-2002, 11:46 PM
Well, to those who posted since my last...yes and no. Here's the other problem...for(var i = 1; k <=2; i++) {
for(j = 1; j<=20; j++) {
var m = oTh[k][j];
m.style.backgroundColor = "red";
}
}
Why k? It never get's incremented by this loop so the outer loop never ends, becaue k never becomes greater than 2. So, all the k's need to be changed to i, or all the i's need to be changed to k :D

glenngv
11-25-2002, 01:14 AM
and shivboy, don't make it hard on yourself.
eval() is unnecessary.


for (var j = 1; j <= 20; j++)
{
oTh[i][j]= "div"+i+"_"+j;
}

shivboy
11-25-2002, 09:31 PM
hi ppl,

thanx a ton for ur help. i rectified those errors, but still one error persists, it says .style is null why so? (check the bold line) is it coz i removed the eval from the code? plz help!

thanx again,
shivboy



var oTh = new Array();
for (var i = 1; i <= 2; i++)
{
oTh[i] = new Array();
for (var j = 1; j <= 20; j++)
{
oTh[i][j]= "div" + i + "_" + j; }
}
for(var i = 1; k <=2; i++)
{
for(j = 1; j<=20; j++)
{
var m = oTh[k][j];
m.style.backgroundColor = "red";
}
}

beetle
11-25-2002, 10:59 PM
You should be using document.getElementById....var oTh = new Array();
for (var i = 1; i <= 2; i++)
{
oTh[i] = new Array();
for (var j = 1; j <= 20; j++)
{
oTh[i][j]= document.getElementById("div" + i + "_" + j);
}
}

shivboy
11-26-2002, 07:15 AM
hi beetle,

thanx for replying. but dont u think using getElementById would make the code ONLY DOM based & not all browser dependent. wat to do then?

thanx again,
shivboy

glenngv
11-26-2002, 07:56 AM
var oTh = new Array();
for (var i = 1; i <= 2; i++)
{
oTh[i] = new Array();
for (var j = 1; j <= 20; j++)
{
oTh[i][j]= "div" + i + "_" + j;
obj = (document.getElementById)?document.getElementById(oTh[i][j]):(document.all)?document.all[oTh[i][j]]:null;
if (obj) obj.style.backgroundColor = "red";
}
}

beetle
11-26-2002, 04:12 PM
better....var oTh = new Array();
for (var i = 1; i <= 2; i++)
{
oTh[i] = new Array();
for (var j = 1; j <= 20; j++)
{
oTh[i][j]= getNode("div" + i + "_" + j);
}
}

function getNode(id) {
return (document.layers) ? document.layers[id] : (document.getElementById || document.all)(id);
}Now you have a re-usable getNode() function :D

shivboy
11-26-2002, 08:42 PM
hi beetle,

thanx for the reply man. but i didnt get what u r trying to do here. could you plz explain this a bit to me? i'll be really grateful.

thanx,
shivboy

kwhubby
11-26-2002, 09:52 PM
how do you make a 2d or 3d array
do have to use all of those loops etc. or can you just do something like var x = new Array(10, 10) ???
or something similar
and than access it something like x[1, 1]???

beetle
11-26-2002, 11:14 PM
Well, the getNode() function can be used instead of document.getElementById or document.all or whatever, because it will use the approriate call depending on what is available, so it's a cross-browser compatible object finder (also called nodes, hence the name) We just need to supply the id.

You CAN define a 2d or 3d array without the looping, but the literal notation is required. Here's a 2d array with the regular assignment syntaxvar data = new Array();
data[0] = new Array();
data[0][0] = 1;
data[0][1] = 2;
data[0][2] = 3;
data[1] = new Array();
data[1][0] = 1;
data[1][1] = 2;
data[1][2] = 3;Pretty complicated, huh? We can make our life simpler with loops, like thisvar data = new Array();
for (var j=0; j<2; j++) {
data[j] = new Array();
for (j=0; j<3; data[j][k] = ++k) {}
}There, much better, but perhaps overkill for a small 2d array like this (2x3) so we can use the literal notation to achieve the same resultvar data = [
[1,2,3],
[1,2,3]
];Nice huh? The literal notation is advantageous where the data is small, or static where the benefits of loops have no weight. Now, if I wanted to change this array to be much bigger, say 10x10, it would be MUCH easier to use the loop. I would just need to change my loop control variablesvar data = new Array();
for (var j=0; j<10; j++) {
data[j] = new Array();
for (k=0; k<10; data[j][k] = ++k) {}
}Whereas with literal notation, I'd need alot more typing, and linesvar data = [
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10],
[1,2,3,4,5,6,7,8,9,10]
];Ok, moving on. Just from the example above, you should see that the notation for accessing a 2d array is:

array[1dIndex][2dIndex];

Now, just to show you what a 3d array looks like in literal notationvar data = [
[
[1,2,3],
[1,2,3],
[1,2,3]
],
[
[1,2,3],
[1,2,3],
[1,2,3]
],
[
[1,2,3],
[1,2,3],
[1,2,3]
],
];This is a simple 3x3x3 array. Here's the same array with loopsvar data = new Array();
for var(j=0; j<3; j++) {
data[j] = new Array();
for (var k=0; k<3; k++) {
data[j][k] = new Array();
for (var l=0; l<3; data[j][k][l] = ++l) {}
}
}It should be evident at this point that loops DEFINITELY have their place when it comes to array creation.