Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

not sure why this is not working - 2d array 1

Status
Not open for further replies.

jamert

Programmer
Dec 9, 2007
80
CA
Hi, i'm not sure why this is not working... my guess is it has to do with accessing the getElementById using the array value..?..?

any thoughts on this, thanks:

Code:
    var arrF = [
	    ['S0live','<%=S0live%>'],
	    ['S1live','<%=S1live%>'],
        ['S2live','<%=S2live%>'],
        ['S3live','<%=S3live%>'],
        ['S4live','<%=S4live%>'],
        ['S5live','<%=S5live%>'],
        ['S6live','<%=S6live%>'],
        ['S7live','<%=S7live%>'],
        ['S8live','<%=S8live%>'],
        ['S9live','<%=S9live%>'],
        ['S10live','<%=S10live%>'],
        ['S11live','<%=S11live%>'],
        ['S12live','<%=S12live%>'],
        ['S13live','<%=S13live%>'],
        ['S14live','<%=S14live%>'],
        ['S15live','<%=S15live%>'],
        ['S16live','<%=S16live%>'],
        ['S17live','<%=S17live%>'],
        ['S18live','<%=S18live%>'],
        ['UM0live','<%=UM0live%>'],
        ['UM1live','<%=UM1live%>'],
        ['UM2live','<%=UM2live%>'],
        ['UM3live','<%=UM3live%>'],
        ['UM4live','<%=UM4live%>'],
        ['UM5live','<%=UM5live%>'],
        ['UM6live','<%=UM6live%>'],
        ['UM7live','<%=UM7live%>'],
        ['UM8live','<%=UM8live%>'],
        ['UM9live','<%=UM9live%>'],
        ['UM10live','<%=UM10live%>'],
        ['UM11live','<%=UM11live%>'],
        ['UM12live','<%=UM12live%>'],
        ['UM13live','<%=UM13live%>'],
        ['UM14live','<%=UM14live%>'],
        ['UM15live','<%=UM15live%>'],
        ['UM16live','<%=UM16live%>'],
        ['UM17live','<%=UM17live%>'],
        ['UM18live','<%=UM18live%>'],
        ['UP0live','<%=UP0live%>'],
        ['UP1live','<%=UP1live%>'],
        ['UP2live','<%=UP2live%>'],
        ['UP3live','<%=UP3live%>'],
        ['UP4live','<%=UP4live%>'],
        ['UP5live','<%=UP5live%>'],
        ['UP6live','<%=UP6live%>'],
        ['UP7live','<%=UP7live%>'],
        ['UP8live','<%=UP8live%>'],
        ['UP9live','<%=UP9live%>'],
        ['UP10live','<%=UP10live%>'],
        ['UP11live','<%=UP11live%>'],
        ['UP12live','<%=UP12live%>'],
        ['UP13live','<%=UP13live%>'],
        ['UP14live','<%=UP14live%>'],
        ['UP15live','<%=UP15live%>'],
        ['UP16live','<%=UP16live%>'],
        ['UP17live','<%=UP17live%>'],
        ['UP18live','<%=UP18live%>'],
        ['AP0live','<%=AP0live%>'],
        ['AP1live','<%=AP1live%>'],
        ['AP2live','<%=AP2live%>'],
        ['AP3live','<%=AP3live%>'],
        ['AP4live','<%=AP4live%>'],
        ['AP5live','<%=AP5live%>'],
        ['AP6live','<%=AP6live%>'],
        ['AP7live','<%=AP7live%>'],
        ['AP8live','<%=AP8live%>'],
        ['AP9live','<%=AP9live%>'],
        ['AP10live','<%=AP10live%>'],
        ['AP11live','<%=AP11live%>'],
        ['AP12live','<%=AP12live%>'],
        ['AP13live','<%=AP13live%>'],
        ['AP14live','<%=AP14live%>'],
        ['AP15live','<%=AP15live%>'],
        ['AP16live','<%=AP16live%>'],
        ['AP17live','<%=AP17live%>'],
        ['AP18live','<%=AP18live%>'],
        ['AM0live','<%=AM0live%>'],
        ['AM1live','<%=AM1live%>'],
        ['AM2live','<%=AM2live%>'],
        ['AM3live','<%=AM3live%>'],
        ['AM4live','<%=AM4live%>'],
        ['AM5live','<%=AM5live%>'],
        ['AM6live','<%=AM6live%>'],
        ['AM7live','<%=AM7live%>'],
        ['AM8live','<%=AM8live%>'],
        ['AM9live','<%=AM9live%>'],
        ['AM10live','<%=AM10live%>'],
        ['AM11live','<%=AM11live%>'],
        ['AM12live','<%=AM12live%>'],
        ['AM13live','<%=AM13live%>'],
	    ['AM14live','<%=AM14live%>'],
        ['AM15live','<%=AM15live%>'],
        ['AM16live','<%=AM16live%>'],
        ['AM17live','<%=AM17live%>'],
        ['AM18live','<%=AM18live%>'],
    ]
    
    for (i=0,max=arrF.length;i<max;i++){ 
         if (arrF[i][1].length > 0) { document.getElementById(arrF[i][0]).value = arrF[i][1]; }
    }
 
What is the error you are getting? What do you mean "does not work"?
 
Just a quick guess w/o seeing the rest of your code or a better explanation of the error:
Code:
        ['AM16live','<%=AM16live%>'],
        ['AM17live','<%=AM17live%>'],
        ['AM18live','<%=AM18live%>'], [!]<-- this comma should not be here[/!]
    ][!];  <-- technically you should have a semi-colon there[/!]

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
First of all I don't know why you want to have a such a large array when you can make a loop function for it, whatever..

Second as kahl said the comma after the last item cannot be there since it means that another item will come after but doesn't.

And third, why do you use the "max" variable in the loop??? And there is no need to check the length of the item either..

Code:
for (i=0,max=arrF.length;i<max;i++){
         if (arrF[i][1].length > 0) { document.getElementById(arrF[i][0]).value = arrF[i][1]; }
}

I would use:

Code:
for (i=0; i<arrF.length; i++) {
document.getElementById(arrF[i][0]).value = arrF[i][1];
}
 
First of all I don't know why you want to have a such a large array when you can make a loop function for it, whatever..

Lowet, it appears that jamert is pulling the info to populate the array from sever generated data.
Code:
        ['S0live','[!]<%=S0live%>[/!]'],
        ['S1live','[!]<%=S1live%>[/!]'],
        ['S2live','[!]<%=S2live%>[/!]'],
        ['S3live','[!]<%=S3live%>[/!]'],
        etc....
Since you can not pass an array of information straight from the server into a javascript variable, you have to pass each element for the array seperately, and then build the array on the front end in javascript. It's pretty messy looking, yes - but there's not really a much better way of doing it. I guess that jamert could concatenate all the data into 1 string, pass that to the client and then split it based on a separator, but that wouldn't run any faster than the way he's already doing it.
And third, why do you use the "max" variable in the loop??? And there is no need to check the length of the item either..
Actually, what jamert has done here is very good practice. When looping through an array element it is faster to assign the length of the array to a static variable and use that in the conditional argument of the for loop instead of checking against the calculated value for the length of the array.

Imagine this scenario:

You have an array that is 1000s of lines long, you loop thru the entire array and do something. The processor is going to go something like this:

[ul]
[li][!]calculate the total length of the array[/!][/li]
[li]check to see if I am less than the calculated value[/li]
[li]do some stuff[/li]
[li][!]calculate the total length of the array[/!][/li]
[li]check to see if I am less than the calculated value[/li]
[li]do some stuff[/li]
[li][!]calculate the total length of the array[/!][/li]
[li]check to see if I am less than the calculated value[/li]
[li]do some stuff[/li]
[li]etc... repeat 1000s of times[/li]
[/ul]

If you store length of the array in a variable first it goes something like this:
[ul]
[li][!]calculate the total length of the array - store in a static variable[/!][/li]
[li]check to see if I am less than the static value[/li]
[li]do some stuff[/li]
[li]check to see if I am less than the static value[/li]
[li]do some stuff[/li]
[li]check to see if I am less than the static value[/li]
[li]do some stuff[/li]
[li]etc... repeat 1000s of times[/li]
[/ul]

As you can see, there's 1000s less calculations done with jamert's method

If you are ever tasked with optimizing the speed of your javascript code, look for your loops and code them similar to what jamert has done - you will be surprised how much faster it is, even if seems like extra unnecessary code.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
I understand. I'm just used to transfer data from the server in a JSON string and not in this way, but it doesn't matter..

Well, about the "max" variable, I just never thought about it that way. I see that it could be useful if the "i" variable could be changed in the loop, so I'll think of it next time.. Thanx kahn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top