Hi,
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