how to parse an entire multi-dimensional array ?

Discussion in 'Javascript' started by iop, Oct 13, 2003.

  1. iop

    iop Guest

    Hello there,
    I'd like to "parse" an entire multi-dimension array like this :
    APP["framework"]["config"]["top"]
    APP["framework"]["config"]["left"]
    without knowing "framework" or "config" or anything passed as variables...
    'cause it's simple to call APP['framework']['config'].length
    My goal is to simply write a xml file out a javascript array...

    Thanks for any help !!

    Stephane.
     
    iop, Oct 13, 2003
    #1
    1. Advertising

  2. Hi,

    iop wrote:

    > Hello there,
    > I'd like to "parse" an entire multi-dimension array like this :
    > APP["framework"]["config"]["top"]
    > APP["framework"]["config"]["left"]
    > without knowing "framework" or "config" or anything passed as variables...
    > 'cause it's simple to call APP['framework']['config'].length
    > My goal is to simply write a xml file out a javascript array...
    >
    > Thanks for any help !!
    >
    > Stephane.


    First, JavaScript doesn't have multi-dimensional arrays. What you have
    here is actually an array of arrays of arrays. It's important to get the
    difference, because it changes the way the array will be parsed.

    Second, when you save array items with labels (making the array a kind
    of Hashtable, though it's not really one), you can parse it with a
    for... in loop, as shown here:

    var astrTest = new Array();
    astrTest["1"] = "Hello";
    astrTest["2"] = "World";
    astrTest["3"] = "Yo";

    for ( var strLabel in astrTest )
    {
    alert( astrTest[ strLabel ] );
    }

    Third, an array is an object with an additional property: "length". This
    allows you to recognize if a given object is an array.

    Fourth, for "pseudo-hashtables", the length is 0 even if the array
    contains items.

    Based on this, I propose a recursive function to parse your array:

    var APP = new Array();

    APP[ "boxer" ] = new Array();
    APP[ "boxer" ][ "dog" ] = new Array();
    APP[ "boxer" ][ "dog" ][ "of" ] = "popular";
    APP[ "boxer" ][ "dog" ][ "opponent" ] = "over";
    APP[ "boxer" ][ "dog" ][ "quick" ] = "quickly";
    APP[ "boxer" ][ "fawn" ] = new Array();
    APP[ "boxer" ][ "fawn" ][ "shot" ] = "the";
    APP[ "boxer" ][ "fawn" ][ "to" ] = "white";
    APP[ "boxer" ][ "fawn" ][ "zinc" ] = "popular";
    APP[ "boxer" ][ "fawn" ][ "belief" ] = "is";
    APP[ "boxer" ][ "fawn" ][ "that" ] = "fornication";
    APP[ "boxer" ][ "fox" ] = new Array();

    APP[ "boxes" ] = new Array();
    APP[ "boxes" ][ "gloved" ] = new Array();
    APP[ "boxes" ][ "his" ] = new Array();

    APP[ "dizzy" ] = new Array();
    APP[ "dizzy" ][ "large" ] = new Array();
    APP[ "dizzy" ][ "lazy" ] = new Array();
    APP[ "dizzy" ][ "mad" ] = new Array();

    APP[ "brown" ] = new Array();
    APP[ "brown" ][ "jab" ] = new Array();
    APP[ "brown" ][ "jab" ][ "would" ] = "be";
    APP[ "brown" ][ "jab" ][ "a" ] = "quick";
    APP[ "brown" ][ "jab" ][ "fix" ] = "for";
    APP[ "brown" ][ "jab" ][ "some" ] = "overzealously";
    APP[ "brown" ][ "jab" ][ "judicious" ] = "governments";
    APP[ "brown" ][ "jaw" ] = new Array();
    APP[ "brown" ][ "jumped" ] = new Array();
    APP[ "brown" ][ "jumps" ] = new Array();

    function parseArray( strUniqueKey, aArrayToParse )
    {
    if ( aArrayToParse == null )
    {
    document.writeln( strUniqueKey + ": Array is null" );
    return;
    }

    if ( aArrayToParse.length == undefined )
    {
    document.writeln( strUniqueKey + ": Parameter is not an array" );
    return;
    }

    var bSomething = false;
    for ( var strKey in aArrayToParse )
    {
    bSomething = true;
    var item = aArrayToParse[ strKey ];

    if ( ( typeof item == "object" )
    && ( item.length != undefined ) )
    {
    parseArray( strUniqueKey + " / " + strKey,
    item );
    }
    else
    {
    document.writeln( "<BR>" );
    document.writeln( strUniqueKey + ": " + item );
    }
    }

    if ( !bSomething )
    {
    document.writeln( "<BR>" + strUniqueKey + ": Array is empty" );
    }

    document.writeln( "<BR>*** " + strUniqueKey + ": done!<HR>" );
    }

    parseArray( "Root", APP );


    Please excuse the crazy data, it was not easy finding enough silly words
    to demonstrate the use of the function :)

    HTH,

    Laurent
    --
    Laurent Bugnion, GalaSoft
    Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion, GalaSoft, Oct 14, 2003
    #2
    1. Advertising

  3. "Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_SPAM.ch> writes:

    > Fourth, for "pseudo-hashtables", the length is 0 even if the array
    > contains items.


    In that case there is no reason to use an Array at all. You could just
    use a plain Object.

    You can still iterate through the added properties using for(..in..).

    > if ( aArrayToParse == null )
    > if ( aArrayToParse.length == undefined )


    When comparing to base values, you should always use non-type-converting
    comparison (===).

    If you don't use arrays, but just objects, you don't need to test for
    the length. You can just test
    typeof input == 'object' && input !== null


    /L
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Oct 14, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kk_oop
    Replies:
    1
    Views:
    2,172
  2. Replies:
    7
    Views:
    853
    Ryan Stewart
    Feb 20, 2005
  3. John Harrison
    Replies:
    2
    Views:
    722
    John Harrison
    Jul 14, 2003
  4. Venkat
    Replies:
    4
    Views:
    994
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    215
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page