Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Iterating through a multi-dimentional array, how to??

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Iterating through a multi-dimentional array, how to??

    Hello all,

    As my title states can anyone tell me how do I iterates through a multi-dimentional array which looks sth like this:

    [1,45,32,["data1","data2",34,3.4]]

    and display all the data sth like this:

    1;45;32;<"data1";...>

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    variable[index[index]] I'm assuming.
    - Mike

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If it's only two dimensions, you can hard-code it:
    Code:
    for(var i = 0; i < arr.length; ++i)
      if(arr[i] instanceof Array)
        for(var j = 0; j < arr[i].length; ++j)
          doSomething(arr[i][j]);
      else doSomething(arr[i]);
    However, for a more robust solution, you'll need a recursive function:
    Code:
    function arrIter(arr) {
      for(var i = 0; i < arr.length; ++i)
        if(arr[i] instanceof Array)
          arrIter(arr[i]);
        else
          doSomething(arr[i]);
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    if(arr[i] instanceof Array)
    The instanceof operator will cause syntax errors on older browsers. The constructor property will work across more browsers:

    Code:
    function isArray(object) {
      return Boolean(object) && (object.constructor == Array);
    }
    If the argument will never be null or undefined, the first sub-expression could be removed.

    Mike

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    While we have the experts assembled here over array objects, I've often wondered if there were:

    1 ) a way to iterate through the array objects that exist on a page without knowing their variable names.

    2 ) to select one or more of them on the basis of their variable name matching a string.

    Number one is more important to me, number two could be fudged out in other ways if number one can be done.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    1 ) a way to iterate through the array objects that exist on a page without knowing their variable names.
    Only if you know where they're stored. If they're all globals, for example, they are properties of the global object, meaning you can do:
    Code:
    var arrs = [];
    for(var i in window)
      if(window[i].constructor === Array)
        arrs.push(window[i]);
    2 ) to select one or more of them on the basis of their variable name matching a string.
    Likewise:
    Code:
    var arrs = [];
    for(var i in window)
      if(window[i].constructor === Array && i.indexOf("myArrVars") !== -1)
        arrs.push(window[i]);
    Of course, in a well-designed script, this should never be necessary.


    Alternatively, you could overwrite Array:
    Code:
    var tArr = function() {
      return (Array.instances[Array.instances.length] = new RealArray);
    };
    
    tArr.instances = [];
    
    RealArray = Array;
    
    Array = tArr;
    This will give you a nice neat array of all instances of Array in Array.instances, but may break some functionality (the Array(x, y, z) array constructor syntax for a start; there's probably a way around this, but I'm not yet sure what it is).
    Last edited by Twey; 10-16-2006 at 05:23 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Cool, but no good in IE - what a surprise! This was interesting:

    Code:
    for(var i in window)
      if(window[i]&&window[i].constructor)
    alert(window[i].constructor);
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Alternatively, you could overwrite Array:
    Code:
    var tArr = function() {
      return (Array.instances[Array.instances.length] = new RealArray);
    };
    
    tArr.instances = [];
    
    RealArray = Array;
    
    Array = tArr;
    This will give you a nice neat array of all instances of Array in Array.instances, but may break some functionality (the Array(x, y, z) array constructor syntax for a start; there's probably a way around this, but I'm not yet sure what it is).
    The only way to directly invoke the constructor function with the new operator and an unknown set of arguments is using eval. However, one could just as easily implement the Array constructor function oneself:

    Code:
    function Array(value) {
        var array = [],
            length;
    
        if ((length = arguments.length))
            if ((length == 1) && (typeof value == 'number'))
                array.length = value >>> 0;
            else
                for (var i = 0; i < length; ++i) array.push(arguments[i]);
        Array.instances.push(array);
        return array;
    }
    Array.instances = [];
    This doesn't exactly mimic the behaviour of the constructor - if the single numeric argument form isn't a 32-bit unsigned integer, an exception should occur - but it's probably close enough (assumng it works at all: it's untested). That said, I wouldn't advise it: create a "static" method of the Array constructor function that adds this behaviour, rather than modifying the constructor itself.

    Mike

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This was interesting:
    And time-consuming, I should think
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey
    And time-consuming, I should think
    Not in IE, unfortunately. I tested in IE, FF and O. It was a rather simple page so, it didn't take too long even in FF which by far had the most responses. Opera was in between.

    Doesn't anybody else learn stuff this way? I don't mean you personally Twey but, I am amazed by how many folks ask if they can do something when just trying it would give them an answer.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •