Solved

# Javascript Tree Traversal

Posted on 2011-03-15
569 Views
0
Question by:JCW2

LVL 20

Accepted Solution

Proculopsis earned 250 total points
ID: 35148418

Do you mean something like this:

``````<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26783203.html</title>
<script type="text/javascript">

var tree = {
myNull: null,
myBoolean: true,
myNumber: 3.1415926535897932,
myString: "Hello World!",
myArray: [ null, true, 3.1415926535897932, "Hello World!" ],
myObject: { myNull: null, myBoolean: true, myNumber: 3.1415926535897932, myString: "Hello World!", myArray: [ null, true, 3.1415926535897932, "Hello World!" ] }
};

\$("#result").text( tree.traverse() );

});

Object.prototype.traverse = function(){
var result = "";
var depth = (arguments.length != 0)? arguments[0] + 1 : 0;
var level = "";
while ( level.length != depth * 2 ) level += "  ";

switch ( type( this ) ) {
case "Boolean":
case "Number":
result += this;
break;

case "String":
result += "\"" + this + "\"";
break;

case "Array":
resolve( this, "[", "]", false );
break;

case "Object":
resolve( this, "{", "}", true );
break;
}

return result;

function resolve( value, prefix, suffix, label ) {
result += prefix + "\r";
for ( entry in value )
if( isSimple( value[entry] ) ){

inset( "  " );
if ( label ) result += "\"" + entry + "\": ";
result += (value[entry] != null)? value[entry].traverse( depth ) : "null";
result += ",\r";
}
result = result.replace( /,\r\$/m, "\r" );
inset( suffix );
}

function type( probe ) {
var theType = "Null";
if ( probe != null ) theType = probe.constructor.toString().replace( /(?:\n|\r|.)*?[ ]{1}([^\(]+)(?:\n|\r|.)*/m, "\$1" );
return theType;
}

function isSimple( value ) {
return "Null,Boolean,Number,String,Array,Object".indexOf( type( value ) ) != -1;
}

function inset( value ) {
result += level + value;
}

}

</script>
<body>

<pre id="result">
</pre>

</body>
</html>
``````
0

LVL 19

Assisted Solution

nschafer earned 250 total points
ID: 35168515
Do you mean making a TreeView in javascript?

If so here is a tutorial I've created on the subject.

http://www.applicationgroup.com/tutorials/treeview/JS_TreeView_Sample.asp

Hope this helps,
Neal.
0

Author Closing Comment

ID: 35222569
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project