SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: Working with Arrays
-
Dec 10, 2003, 10:26 #1
- Join Date
- Sep 2003
- Location
- At work
- Posts
- 371
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Working with Arrays
Hi, I've some problem with arrays and hope someone here could help.
I have a dynamically generated array below:
Code:var ar = new Array( ); ar[4] = new Array( ); ar[4][0] = new Option( "Main Disk", "0" ); ar[4][1] = new Option( "Root folder", "1" ); ar[4][2] = new Option( "Inside Root Folder", "3" ); ar[4][3] = new Option( "inside Inside Root", "4" );
My problem is, I need to first know what ar[x] will contain during dynamic (in this case, its 4). if not, I've no idea how to loop it. Anyone know a way to solve this?
Thanks in advanced.
-
Dec 10, 2003, 10:53 #2
- Join Date
- Dec 2003
- Location
- A van down by the river
- Posts
- 2,056
- Mentioned
- 0 Post(s)
- Tagged
- 1 Thread(s)
not quite sure what you're asking...
I'll assume you have something like:
for( var i = 0; i < 4; ++i )...?
change it to:
for( var i = 0; i < ar[4].length; ++i )...
to loop through the elements of the 5th element of the ar array.
-
Dec 10, 2003, 11:01 #3
- Join Date
- Sep 2003
- Location
- At work
- Posts
- 371
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi rushiku, thanks for the reply. Yes, this works of course, but during runtime, the array is dynamically generated by backend script and ar[] can be some other numbers. That is the problem where I need to somehow get the numeric value of ar so as I can loop thru it.
-
Dec 10, 2003, 11:08 #4
- Join Date
- Sep 2003
- Location
- At work
- Posts
- 371
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
sorry if I didn't explain it well.
here is the starting of my loop
Code:for( i=0; i<ar.length; i++ ) { if( ar[i][i].value == dir ) {
-
Dec 10, 2003, 12:09 #5
- Join Date
- Dec 2002
- Location
- Alabama, USA
- Posts
- 2,560
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I agree with rushiku, we don't yet have enough information about what you're doing. Is the page online?
For looping thru the entire 'ar' array, try this:
Code:var row, col; for (row = 0; row < ar.length; ++row) { for (col = 0; ar[row] && col < ar[row].length; ++col) { alert(ar[row][col]); } }
Cross-Browser.com, Home of the X Library
-
Dec 11, 2003, 00:52 #6
- Join Date
- Sep 2003
- Location
- At work
- Posts
- 371
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by MikeFoster
Bookmarks