{"id":77,"date":"2009-07-28T16:10:33","date_gmt":"2009-07-28T22:10:33","guid":{"rendered":"http:\/\/www.crccheck.com\/blog\/?p=77"},"modified":"2015-10-11T22:30:51","modified_gmt":"2015-10-12T04:30:51","slug":"print_r-for-javascript","status":"publish","type":"post","link":"https:\/\/www.crccheck.com\/blog\/print_r-for-javascript\/","title":{"rendered":"print_r() for javascript"},"content":{"rendered":"<p>I typically just use uneval() to figure out what&#8217;s inside an array\/object, but what about when it&#8217;s large and heterogeneous? I wanted to find a version of php&#8217;s <a href=\"http:\/\/us.php.net\/%20print_r\" target=\"_blank\">print_r()<\/a> for JavaScript. Here is link to the <a href=\"http:\/\/www.openjs.com\/scripts\/others\/dump_function_php_print_r.php\" target=\"_blank\">Original Version of dump()<\/a> I based mine off of:<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.openjs.com\/scripts\/others\/dump_function_php_print_r.php\" target=\"_blank\">www.openjs.com\/scripts\/others\/dump_function_php_print_r.php<\/a><\/p>\n<p style=\"text-align: left;\">When I tried it, the first thing I noticed what that it put quotes around everything, and that long strings with line breaks got messy, so I did a quick adaptation to suit my immediate needs and came up with this:<\/p>\n<pre>function dump(arr,level) {\r\n  function magicquotes(value) { return (isNaN(value)) ? '\"' + value.replace(\/\\n\/g,\"\\n\"+indent) + '\"' : value; }\r\n  level = level | 0;\r\n  var indent = new Array(level+1).join(\"\\t\"), dumped_text = \"\";\r\n\r\n  if(typeof(arr) == 'object') { \/\/Array\/Hashes\/Objects\r\n    for(var item in arr) {\r\n      var value = arr[item];\r\n\r\n      if(typeof(value) == 'object')\r\n          dumped_text += indent + \"'\" + item + \"' :\\n\" + dump(value,level+1);\r\n      else\r\n          dumped_text += indent + \"'\" + item + \"' =&gt; \" + magicquotes(value) + \"\\n\";\r\n    }\r\n  } else { \/\/Stings\/Chars\/Numbers etc.\r\n    dumped_text = \"===&gt;\"+arr+\"&lt;===(\"+typeof(arr)+\")\";\r\n  }\r\n  return dumped_text;\r\n}<\/pre>\n<p>I also found links to many other print_r(), var_dump() equivalents, but they either depended on write, were overly complicated, or returned a lot of excess text I wasn&#8217;t interested in.<\/p>\n<p>So why not just call it print_r ? Well, the original I copied was called dump, and I&#8217;ve always been annoyed typing that underscore, so I just didn&#8217;t change it.<\/p>\n<p>Update: found another alternative. It&#8217;s really long and puts out a lot of extraneous information, but it&#8217;s worth looking at: <a href=\"http:\/\/wiki.greasespot.net\/Code_snippets#Dump_the_properties_of_an_object\">wiki.greasespot.net\/Code_snippets#Dump_the_properties_of_an_object<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I typically just use uneval() to figure out what&#8217;s inside an array\/object, but what about when it&#8217;s large and heterogeneous? I wanted to find a version of php&#8217;s print_r() for JavaScript. Here is link to the Original Version of dump() I based mine off of: www.openjs.com\/scripts\/others\/dump_function_php_print_r.php When I tried it, the first thing I noticed&hellip;<\/p>\n <a href=\"https:\/\/www.crccheck.com\/blog\/print_r-for-javascript\/\" title=\"print_r() for javascript\" class=\"entry-more-link\"><span>Read More<\/span> <span class=\"screen-reader-text\">print_r() for javascript<\/span><\/a>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[5,4],"tags":[],"class_list":["entry","author-showmewhatyougot","post-77","post","type-post","status-publish","format-standard","category-drafts","category-technical"],"_links":{"self":[{"href":"https:\/\/www.crccheck.com\/blog\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.crccheck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.crccheck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.crccheck.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.crccheck.com\/blog\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":8,"href":"https:\/\/www.crccheck.com\/blog\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":776,"href":"https:\/\/www.crccheck.com\/blog\/wp-json\/wp\/v2\/posts\/77\/revisions\/776"}],"wp:attachment":[{"href":"https:\/\/www.crccheck.com\/blog\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.crccheck.com\/blog\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.crccheck.com\/blog\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}