469,323 Members | 1,574 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

how to parse an entire multi-dimensional array ?

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.

Jul 20 '05 #1
2 10453
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

Jul 20 '05 #2
"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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by josh dismukes | last post: by
6 posts views Thread by Dave Kuhlman | last post: by
reply views Thread by Klaus Bonadt | last post: by
1 post views Thread by Aaron Guo | last post: by
19 posts views Thread by 叮叮当当 | last post: by
6 posts views Thread by =?Utf-8?B?RGF2aWRN?= | last post: by
4 posts views Thread by ShutterMan | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.