473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 10660
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: josh dismukes | last post by:
/// here is the code i'm getting a parse error on the last line of the code which /// is </html> any help will be much appreciated. <?php session_start ();
1
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one...
6
by: Dave Kuhlman | last post by:
Suppose that I have content that looks like what I've included at the end of this message. Is there something in the standard Python library that will help me parse it, break into the parts...
0
by: Klaus Bonadt | last post by:
I would like to parse an email, which I receive from a pop3 account. While the subject and the sender's email address are probably easy to determine, the body seems to be more difficult because of...
1
by: Aaron Guo | last post by:
i want to upload a very large file throught http request. on server side , i add a http module to handle the upload request. and i can get the entire data stream by HttpWorkerRequest. i wonder...
19
by: 叮叮当当 | last post by:
hi, all when a email body consist with multipart/alternative, i must know when the boundary ends to parse it, but the email lib have not provide some function to indicate the boundary end,...
0
by: elphantasmo | last post by:
Hi, I have created a hosted AxWebBrowser control and I handle the NewWindow2 event to open new windows in my own forms. Our application accesses a JSF (Java Server Faces) backend with some...
11
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which...
6
by: =?Utf-8?B?RGF2aWRN?= | last post by:
Hello, I have an XML file generated from a third party application that I would like to parse. Ideally, I plan on having a windows service setup to scan various folders for XML files and parse the...
4
by: ShutterMan | last post by:
I have a JSON object as below (data is from SQL Server Northwind Database). But doing an eval on it returns an error "unterminated string constant" or such. Can someone help me pinpoint the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.