Geeks With Blogs

@scottvanvliet
  • scottvanvliet @242_girl @jeb_ It's our pleasure! There may be a few more things in store for you when I come back to Stockholm ;) about 19 hours ago
  • scottvanvliet Is anyone out their using FreeNX on their Amazon Linux flavored #ec2 instances? If so, how do you like it? about 1 day ago
  • scottvanvliet I'm enamored of #svn2git! Migrate FULL version history from SVN repo to git. Posted 2 years of history to @github! https://t.co/vNSnEVQRBU about 1 day ago
  • scottvanvliet I can't explain it, but I feel much more organized when my notebook is quadrille ruled. about 2 days ago

Scott Van Vliet Once a Developer, Always a Developer

In my years of doing Web development, I've written this function several times over; however, I can NEVER find the code when I need it!  Thus, here is a simple Javascript function to dump the contents of any object (posted to share, but also for my own permanent reference!)

 

<script language="javascript">

      

       var MAX_DUMP_DEPTH = 10;

      

       function dumpObj(obj, name, indent, depth) {

              if (depth > MAX_DUMP_DEPTH) {

                     return indent + name + ": <Maximum Depth Reached>\n";

              }

              if (typeof obj == "object") {

                     var child = null;

                     var output = indent + name + "\n";

                     indent += "\t";

                     for (var item in obj)

                     {

                           try {

                                  child = obj[item];

                           } catch (e) {

                                  child = "<Unable to Evaluate>";

                           }

                           if (typeof child == "object") {

                                  output += dumpObj(child, item, indent, depth + 1);

                           } else {

                                  output += indent + item + ": " + child + "\n";

                           }

                     }

                     return output;

              } else {

                     return obj;

              }

       }

      

</script>

 

This will spit out the contents of the object (and nested objects) to an indented string, which is useful when trying to inspect the contents of DOM, etc.  Note that a recursive method like this can be dangerous, so I’ve included a maximum depth flag, MAX_DUMP_DEPTH, to ensure your browser doesn’t get locked up in an infinite nested loop.

 

Enjoy!

Posted on Thursday, March 23, 2006 6:04 PM Javascript , Technology | Back to top


Comments on this post: Simple Javascript Object Dump Function

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Thank you. I have also rewritten this function many times.

Left by Jim on Oct 30, 2006 8:02 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Thought u'd like it.
Left by mozilla on Mar 23, 2007 1:20 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Hi, Firefox says it's a recursive loop even when I put the max depth to 1.

Any ideas?
Left by Paul Feakins on Jul 02, 2007 9:49 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Wonderful, thanks! I've not only written this many times, but have forgotten how to write it even more times than that. Found yours searching for how to write it yet again...thank you.
Left by http:// on Dec 22, 2007 1:03 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
eh.. how do you use this?? I could of swore javascript had an inbuilt console write/dump() function
Left by http:// on Feb 07, 2008 5:57 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Beautiful! Got my JS code to work in minutes with this lil' snippet instead of hours. Major kudos.
Left by Steve Wamsley on Sep 22, 2008 2:53 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
I got the following exception when runningitunderfirefox 3.0.3

Error: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMHistory.length]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: chrome://testextension/content/overlay.js :: dumpObj :: line 76" data: no]
Source File: chrome://testextension/content/overlay.js
Line: 76

the line in question is "for (var item in obj)"

any ideas? will also look in javascript docs, but fear it is something to do with the firefox 3 implementation of javascript (1.8)

Left by http:// on Oct 04, 2008 12:39 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
my object contain array, so i modified my code to this:

function dumpObj(obj, name, indent, depth) {
var MAX_DUMP_DEPTH = 10;

if (depth > MAX_DUMP_DEPTH) {
return indent + name + ": <Maximum Depth Reached>\n";
}
if (typeof obj == "object") {
var child = null;
var output = indent + name;
var total = 0;
if (obj instanceof Array)
{
total = obj.length;
output += " (Array)\n";
}
else
{
for (var item in obj)
{
total++;
}
output += " (Object)\n";
}
output += indent + "Total item: " + total + "\n";
indent += "\t";
if (obj instanceof Array)
{
for (var i = 0; i < obj.length; i++)
{
child = obj[i];
output += dumpObj(child, i, indent, depth + 1);
}
}
else
{
for (var item in obj)
{
try {
child = obj[item];
}
catch (e) {
child = "<Unable to Evaluate>";
}
if (typeof child == "object") {
output += dumpObj(child, item, indent, depth + 1);
}
else {
output += indent + item + ": " + child + "\n";
}
}
}
return output;
}
else {
return obj + " is not an object.";
}
}


this is sample of my object in json:
{"vTT":[{"TTID":"TT050722-400","RealTTID":"000000000002700"},{"TTID":"TT050719-400","RealTTID":"000000000001519"},{"TTID":"TT050725-1400","RealTTID":"000000000004097"},{"TTID":"TT050725-1400","RealTTID":"000000000004098"},{"TTID":"TT081124-400","RealTTID":"000000000007027"}]}
Left by http:// on Jan 08, 2009 8:10 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
If you're developing under Firefox, the Firebug addon is a good tool to have. It has a DOM console that lets you see pretty much every data structure that comes into play in the execution of your code. You can tell it to hide DOM properties and functions, and you can tell it to hide user functions and only show properties.
I find this easier than dumping to the page.
Left by http:// on May 20, 2009 9:58 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Too good .. was looking for such a fucntion since long ..
thanks .. :)
Left by http:// on Sep 16, 2009 10:28 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Great function! Thanks.

I'm a stickler for easy "dump"ing functions, so I added these few lines to the top so the name and indent params get set automatically if you don't want to write them on each function call:

name = name ? name : "Object:";
indent = indent ? indent : "";


Thanks again!
Left by John Sanders on Jun 09, 2010 1:33 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Very useful. Thanks for sharing this.
Left by Khoa on Aug 17, 2010 6:15 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Shouldn't "depth" be decremented on each pass?
Left by Tarkin on Dec 30, 2010 7:41 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Thank You very much!
You saved my Day!

Left by Juergen on Aug 21, 2012 5:48 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Thanks, really useful! :)
Left by Piero on Apr 04, 2013 4:27 PM

Your comment:
 (will show your gravatar)
 


Copyright © svanvliet | Powered by: GeeksWithBlogs.net | Join free