brandnewbox.co.uk

Home of Andrew Weaver, a uk-based web developer specialised in building high-quality websites

A print_r equivalent for Javascript

Tuesday, October 5th, 2004

Andrew Weaver

Category:

A javascript function to emulate PHP's print_r function.

function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}

print_r(prods);

Comments

1. .(JavaScript must be enabled to view this email address)
4th Jan 2006 at 11:14 am

I google "javascript print_r" and I found BrandNewBox. It's almost same as what I really wanted. So I changed a little bit, and put it here.

2. Patrick Nelson
29th Nov 2006 at 9:15 pm

Thanks, this could be useful!

3. Patrick Nelson
29th Nov 2006 at 9:18 pm

BTW, if I use this script, I’ll include the credit to this site. Thanks again.

4. Cooler
11th Feb 2007 at 3:56 pm

It’s nice. Thank you!
Some my correctives.
function print_r(theObj){
  if(theObj.constructor == Array ||
    theObj.constructor == Object){
  (”<ul>”)
  for(var p in theObj){
    if(theObj[p].constructor == Array||
      theObj[p].constructor == Object){
(”<li>[”+p+”] => “+typeof(theObj)+”</li>”);
      (”<ul>”)
      print_r(theObj[p]);
      (”</ul>”)
    } else {
(”<li>[”+p+”] => “+theObj[p]+”</li>”);
    }
  }
  (”</ul>”)
  }
}

5. Patrick Nelson
10th Feb 2008 at 9:16 pm

Eh, I call bullshit on that last comment.

6. Binny V A
1st Mar 2008 at 7:31 am

Could you format the code a bit - it looks very messy.

Also, I made another script with the same functionality…
http://www.openjs.com/scripts/others/dump_function_php_print_r.php

7. .(JavaScript must be enabled to view this email address)
20th Mar 2008 at 8:07 pm

function str_repeat(str, repeat) {
  var output = ‘’;
  for (var i = 0; i < repeat; i++) {
    output += str;
  }
  return output;
}


var MAX_DEPTH = 10;
function print_r(obj, indent, depth) {
  var ws = ‘    ’; //four whitespaces
  var output = ‘’;
  indent = (!indent) ? 0 : indent;
  depth = (!depth) ? 0 : depth;
  if (depth > MAX_DEPTH) {
    return str_repeat(ws, indent) + ‘*Maximum Depth Reached*\n’;
  }
  if (typeof(obj) == “object”) {
    output += (indent == 0) ? typeof(obj) + ‘\n(\n’ : ‘’;
    indent++;
    var child = ‘’;
    for (var key in obj) {
      try {
        child = obj[key];
      }
      catch (e) {
        child = ‘*Unable To Evaluate*’;
      }
      output += str_repeat(ws, indent) + ‘[’+key+’] => ‘;
      if (typeof(child) == “object”) {
        indent++;
        output += typeof(child) + ‘\n’;
        output += str_repeat(ws, indent) + ‘(\n’;
        output += print_r(child, indent, depth+1);
        output += str_repeat(ws, indent) + ‘)\n’;
        indent—;
      }
      else {
        output += child + ‘\n’;
      }
    }
    indent—;
    output += (indent == 0) ? ‘)\n’ : ‘’;
    return output;
  }
  else {
    return str_repeat(ws, indent) + obj + ‘\n’;
  }
}

8. .(JavaScript must be enabled to view this email address)
24th Jul 2008 at 12:27 pm

brilliant

9. .(JavaScript must be enabled to view this email address)
29th Oct 2008 at 7:53 am

Thanks,it is of great use to me.

10. Alain Duchesneau
28th Nov 2008 at 9:20 pm

function print_r(theObj,indent){
      var output=”“;
      if (indent == undefined) { indent = ”  “; } else { indent += ”  “; }
      if(theObj.constructor == Array || theObj.constructor == Object) {
        for(var p in theObj){
          if(theObj[p].constructor == Array|| theObj[p].constructor == Object){
              var type = (theObj[p].constructor == Array) ? “Array” : “Object”;
              output += indent+”[”+p+”](”+type+”)=>\n”;
              output += print_r(theObj[p],indent);
          } else { output += indent+”[”+p+”]:”+theObj[p]+”\n”; }
        }
      }
      return output;
  }

11. Clifford James
1st Mar 2009 at 9:49 pm

I’ve made an script wich copies the exact functionality as PHP’s print_r function.
go check it out!

http://cjdev.wordpress.com/2009/02/25/phps-print_r-function-in-javascript/

Add a comment

 
Remember my personal information
 
Notify me of follow-up comments?
 

© brandnewbox.co.uk 2004-2008