how to parse an entire multi-dimensional array ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iop

    how to parse an entire multi-dimensional array ?

    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.



  • Laurent Bugnion, GalaSoft

    #2
    Re: how to parse an entire multi-dimensional array ?

    Hi,

    iop wrote:
    [color=blue]
    > 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.[/color]

    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" ] = "fornicatio n";
    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" ] = "government s";
    APP[ "brown" ][ "jaw" ] = new Array();
    APP[ "brown" ][ "jumped" ] = new Array();
    APP[ "brown" ][ "jumps" ] = new Array();

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

    if ( aArrayToParse.l ength == undefined )
    {
    document.writel n( 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.writel n( "<BR>" );
    document.writel n( strUniqueKey + ": " + item );
    }
    }

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

    document.writel n( "<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

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: how to parse an entire multi-dimensional array ?

      "Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_S PAM.ch> writes:
      [color=blue]
      > Fourth, for "pseudo-hashtables", the length is 0 even if the array
      > contains items.[/color]

      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..).
      [color=blue]
      > if ( aArrayToParse == null )
      > if ( aArrayToParse.l ength == undefined )[/color]

      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 - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...