andrew4582

outputObject Javascript

Nov 6th, 2011
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /*
  2.  
  3.     Ouputs the object's property and values to an string array
  4.    
  5.     Example:
  6.  
  7.     var theobj = {
  8.     name: "Joe",
  9.     phone: "(713) 987-3652"
  10.     };
  11.    
  12.     var arr = outputObject(theobj);
  13.     document.write(arr.join(""));
  14.  
  15.     */
  16.     function outputObject(settings, ignoreFunctions, showobj) {
  17.         var html = [];
  18.  
  19.         html.push("<ol>");
  20.  
  21.         for (key in settings) {
  22.             var val = settings[key];
  23.             var isfunc = typeof (val) === "function";
  24.             var isobj = typeof (val) == "object";
  25.             var typePart = "&nbsp;&nbsp; [" + typeof (val) + "]";
  26.  
  27.             if (ignoreFunctions && isfunc) continue;
  28.             if (isobj) typePart = "";
  29.  
  30.             var clr = "green";
  31.             if (val == null || val == undefined) clr = "red";
  32.             else if (isobj) clr = "blue";
  33.  
  34.             html.push("<li style='color:" + clr + ";'>" + [key] + "&nbsp;=&nbsp;" + val + typePart);
  35.             if (!showobj) continue;
  36.  
  37.             if (isobj) {
  38.                 var subref = val;
  39.  
  40.                 html.push("<ul>");
  41.                 for (sub in subref) {
  42.                     var subval = subref[sub];
  43.                     var issubfunc = typeof (subval) === "function";
  44.                     if (ignoreFunctions && issubfunc) continue;
  45.                     var subtypePart = "&nbsp;&nbsp; [" + typeof (subval) + "]";
  46.  
  47.                     html.push("<li style='color:#8600ff;'>" + [sub] + "&nbsp;=&nbsp;" + subval + subtypePart + "</li>");
  48.                 }
  49.                 html.push("</ul>");
  50.             }
  51.             html.push("</li>");
  52.         }
  53.         html.push("</ol>");
  54.         return html;
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment