[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Javascript Tree Traversal

Posted on 2011-03-15
3
Medium Priority
?
573 Views
Last Modified: 2012-05-11
Can you demonstrate tree traversal in Javascript on this page?
0
Comment
Question by:JCW2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Accepted Solution

by:
Proculopsis earned 1000 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>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26783203.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

jQuery(document).ready( function () {

  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>
</head>
<body>

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

</body>
</html>

Open in new window

0
 
LVL 19

Assisted Solution

by:nschafer
nschafer earned 1000 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

by:JCW2
ID: 35222569
Thank you for your help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question