Geeks With Blogs
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...
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...
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

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Thank you so much, I have been using this function till now.

The best black google search engine online that saves energy.
Left by Joel on May 26, 2015 10:30 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
It works fine. This function is easier to follow. - Edmond Dentist
Left by William Wright on Nov 17, 2015 4:03 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Great Post and Nicely Explained Emojio
Left by Pritam SIngh on Oct 01, 2016 7:40 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Thanks for this Great Article Pokemon Calculator
Left by Pritam SIngh on Oct 01, 2016 7:41 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...


function mydump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";

if(typeof(arr) == 'object') {
for(var item in arr) {
var value = arr[item];

if(typeof(value) == 'object') {
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += mydump(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
}
}
} else {
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}

how about this one?

regards from mehndi designs
Left by framaroot apk on Nov 09, 2016 2:29 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Easily build 25 UNIQUE backlinks to your pages every day! Sign up now and start buy backlinks generating quality inbound links to your site today!
Left by backlinks seo on Nov 29, 2016 10:04 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Teeth Dreams are one of the most common dreams that we experience. Teeth dreams dream losing teeth are scary and we get them in the form of loosing, crumbling, broken, ...
Left by dream losingteeth on Nov 30, 2016 10:10 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
LiLi USB Creator is a handy, easy to linuxlive usb creator use application designed to enable you to create a bootable Live USB key with a Linux on it....
Left by linux liveusb on Nov 30, 2016 11:07 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
We reveal how to navigate five snapchat video chat of Snapchat's most exciting bonus features. ... button and don't let go, your screen will become a live video chat.
Left by snapchat videochat on Dec 01, 2016 1:39 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Program Description. Performs disk speed test and provided details about how fast your disk speed test disks can transfer data. The information provided includes data transfer ...
Left by disk speed test on Dec 01, 2016 2:43 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Responsive Design significa que usted puede administrar un sitio web con un solo conjunto de enlaces de hipertexto; Reduciendo así el tiempo dedicado a mantener su sitio. Esto le permite centrarse en el alcance de enlace con una consolidada estrategia de optimización de motores de búsqueda (SEO). Las campañas de una agencia de SEO en Mexico pueden consumir mucho tiempo y son costosas, pero al crear un sitio de respuesta, todos sus esfuerzos pueden concentrarse en un solo sitio, con estrategias y tácticas unificadas entre dispositivos.
Left by Roberto on Jan 13, 2017 10:09 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
A dental crown (or cap) is a covering that encases the complete tooth floor restoring it to its unique form and measurement. A crown placed by an expert dentist in Mexico protects and strengthens tooth structure that can't be restored with fillings or different forms of restorations. Although there are a number of forms of crowns, porcelain (tooth coloured crown) are the most well-liked, as a result of they resemble your pure tooth. They are highly durable and will final a few years, but like most dental restorations, they could ultimately need to be replaced. Porcelain crowns are made to match the form, dimension, and color or your enamel supplying you with a pure, long-lasting lovely smile.
Left by Carlos on Jan 30, 2017 12:01 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Left by eshaan on Mar 15, 2017 10:38 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
cám ơn bạn đã chia sẻ bài viết thật tuyệt với
nếu bạn có nhu cầu mua nam giam can thì lien he website thuocngoai.vn nhé
Left by thuoc giam can on Apr 06, 2017 9:20 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
i m Very weak in creating java scripting, i still needs to do more practice on java script, i m learning it from simple Mehndi Design art school
Left by Sam Mak on Apr 16, 2017 10:09 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Really interesting stuff you mention here.

Dentist in Tijuana Mexico
Left by Neter on Apr 17, 2017 10:45 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
really interesting stuff, tks
http://phukhoathaiha.org/
Left by raijin on May 02, 2017 12:47 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Our company, Bariatric Surgery 4 Health, represents bariatric surgeons in Tijuana, Mexico. Although our surgeons have a lot of experience with various types of bariatric surgery for weight loss, such as the gastric bypass, and gastric banding, the most common surgery that he does is the vertical gastric sleeve.
Left by NeterMedia on May 02, 2017 6:26 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
That's a nice function. I will use it on my torrent search engine website, Torstatus
Left by Pirates on May 03, 2017 3:49 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
really interesting stuff, tks Phúc Thịnh
Left by kappe on May 18, 2017 4:35 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Neu nhu ban muon xem video may loc nuoc ion kiem thi vui long truy cap vao dia chi may loc nuoc ion kiem giau hydro
Left by hieunguyen on May 26, 2017 2:21 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
NIce simple and sweet sexy post http://shahistar.com/ hot images.
Left by shahazaha on Jul 14, 2017 12:26 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Good Providing Beautiful Women Pics www.beautyelife.com
Left by mewafo on Jul 20, 2017 10:13 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Left by menao on Jul 21, 2017 9:27 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Thanks for this object dump function.
I'll try this on my drone application.
Best regards: <a herf="http://www.testmateriel.net/meilleurs-drones-camera/>
Left by Pilot on Jul 29, 2017 5:06 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Thanks for this object dump function.
I'll try this on my drone application.
Best regards:
Left by Pilot on Jul 29, 2017 5:07 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Really interesting stuff, thank you.
[URL='http://pkbenhxahoi.vn/phong-kham-da-khoa-thai-ha-dia-chi-kham-benh-tin-cay-12WEFAWZ.html']phòng khám thái hà lừa đảo[/URL]
Left by Peter on Aug 14, 2017 1:01 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Really interesting stuff, thank you.
http://pkbenhxahoi.vn/phong-kham-da-khoa-thai-ha-dia-chi-kham-benh-tin-cay-12WEFAWZ.html
Left by Peter on Aug 14, 2017 1:04 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
If you are looking for the best cold press juicer there are several option here
Left by kent on Aug 14, 2017 6:37 PM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
i want to be a java developer can you please suggest me any good java learning course? All Health Post
Left by Andrew on Sep 15, 2017 12:31 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Check here What is Flonase (Nasal Spray)?
Left by Andrew on Sep 15, 2017 12:34 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Left by Andrew on Sep 15, 2017 12:35 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
Left by Andrew on Sep 15, 2017 12:36 AM

# re: Simple Javascript Object Dump Function
Requesting Gravatar...
There are two main types of stroke <a href"="https://allhealthpost.com/stroke-symptoms/">What are they?
Left by Andrew on Sep 15, 2017 12:37 AM

Your comment:
 (will show your gravatar)


Copyright © svanvliet | Powered by: GeeksWithBlogs.net