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.
4th Jan 2006 at 7: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 5:15 pm

Thanks, this could be useful!

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

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

4. Cooler
11th Feb 2007 at 11:56 am

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 5:16 pm

Eh, I call bullshit on that last comment.

6. Binny V A
1st Mar 2008 at 3: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.
20th Mar 2008 at 4: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’;
  }
}

Add a comment

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

© brandnewbox.co.uk 2004-2008