473,785 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10688
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" ] = "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

Jul 20 '05 #2
"Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_S PAM.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.l ength == 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
2932
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
2402
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 entire line from the file , headers as well > > >as just the desired body messages . > > > > > >fp = file("/home/chuck/pythonScript/testbox") > > >mb = mailbox.UnixMailbox(fp, > > >email.message_from_file)
6
9715
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 separated by the boundary strings, extract headers from each sub-part, etc? Do I need to add something like the following to the beginning? Content-Type: multipart/related; type="multipart/alternative";
0
2342
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 multi-part messages in MIME format. I am interested only in the plain text. Do you know an easy way to retrieve plain text of the body from an email message? Maybe a class, which can be constructed using the whole message string as a parameter...
1
2113
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 that how to parse the entire data stream? i do not know what the data format there is. thanks very much.
19
2597
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, how to solve it ? thanks.
0
1958
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 custom controls created by Oracle (specifically a date/calendar popup to select a date). These custom controls behave in a strange way, i.e. they "fade" the parent window, make the child popup modal but apparently not by using "showModalDialog". It...
11
3544
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 returns a elementtree, on which you can then apply xpath; 2) To parse a string (xml section), you can call XML or fromstring, but both return element instead of elementtree. This alone is bad. To make it worse, you have to create an elementtree from...
6
2977
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 file, then spit out totals. Since I haven't worked with XML too much in C#, I'm trying to develop a structured and easy-to-read way to parse the file. Essentially, I would like to read the file and add the "BatchTktAmountfor any...
4
2399
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 element that is causing the grief? Sorry to post the entire object, but Im hoping someone has a parse utility that will help find whats wrong. { "TABLE":}, { "COL":"},{"DATA":"Education includes a BA in psychology from Colorado State University in...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.