how to parse an entire multi-dimensional array ?

I

iop

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.
 
L

Laurent Bugnion, GalaSoft

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
 
L

Lasse Reichstein Nielsen

Laurent Bugnion said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top