SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict say's Avatar
    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" );
    For example, when I choose "Inside Root Folder", I need to loop thru the above array from the start and ends after it meets "Inside Root Folder".

    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.

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    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.

  3. #3
    SitePoint Addict say's Avatar
    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.

  4. #4
    SitePoint Addict say's Avatar
    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 )
    		{
    This gives me an error on ar.length is null. But the reason I do that is because I don't know what the numeric value will be when the script runs as it is dynamic.

  5. #5
    I'll take mine raw silver trophy MikeFoster's Avatar
    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]);
      }
    }

  6. #6
    SitePoint Addict say's Avatar
    Join Date
    Sep 2003
    Location
    At work
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster
    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]);
      }
    }
    hehe, sorry. yeah, I know, I can't agree more that I didn't explain it well. Javacript is just driving me crazy. Thanks for the help rushiku and mike. I got it work out with a little will power.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •