SHARE
TWEET

Untitled

a guest Jun 20th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. console.log(obj)
  2.      
  3. var obj = {prop1: 'prop1Value', prop2: 'prop2Value', child: {childProp1: 'childProp1Value'}}
  4. console.log(obj)
  5.      
  6. console.log('My object : ' + obj)
  7.      
  8. str = JSON.stringify(obj);
  9. str = JSON.stringify(obj, null, 4); // (Optional) beautiful indented output.
  10. console.log(str); // Logs output to dev tools console.
  11. alert(str); // Displays output using window.alert()
  12.      
  13. obj = JSON.parse(str); // Reverses above operation (Just in case if needed.)
  14.      
  15. "Uncaught TypeError: Converting circular structure to JSON"
  16.      
  17. var output = '';
  18. for (var property in object) {
  19.   output += property + ': ' + object[property]+'; ';
  20. }
  21. alert(output);
  22.      
  23. console.log(JSON.stringify(obj))
  24.      
  25. var print = function(o){
  26.     var str='';
  27.  
  28.     for(var p in o){
  29.         if(typeof o[p] == 'string'){
  30.             str+= p + ': ' + o[p]+'; </br>';
  31.         }else{
  32.             str+= p + ': { </br>' + print(o[p]) + '}';
  33.         }
  34.     }
  35.  
  36.     return str;
  37. }
  38.      
  39. var myObject = {
  40.     name: 'Wilson Page',
  41.     contact: {
  42.         email: 'wilson@hotmail.com',
  43.         tel: '123456789'
  44.     }  
  45. }
  46.  
  47. $('body').append( print(myObject) );
  48.      
  49. console.table(obj);
  50.      
  51. console.table(obj, ['firstName', 'lastName']);
  52.      
  53. var getPrintObject=function(object)
  54. {
  55.     return JSON.stringify(object);
  56. }
  57.      
  58. console.log('print object: ' + JSON.stringify(session));
  59.      
  60. console.dir(object, {depth: null, colors: true})
  61.      
  62. // Recursive print of object
  63. var print = function( o, maxLevel, level ) {
  64.     if ( typeof level == "undefined" ) {
  65.         level = 0;
  66.     }
  67.     if ( typeof level == "undefined" ) {
  68.         maxLevel = 0;
  69.     }
  70.  
  71.     var str = '';
  72.     // Remove this if you don't want the pre tag, but make sure to remove
  73.     // the close pre tag on the bottom as well
  74.     if ( level == 0 ) {
  75.         str = '<pre>';
  76.     }
  77.  
  78.     var levelStr = '';
  79.     for ( var x = 0; x < level; x++ ) {
  80.         levelStr += '    ';
  81.     }
  82.  
  83.     if ( maxLevel != 0 && level >= maxLevel ) {
  84.         str += levelStr + '...</br>';
  85.         return str;
  86.     }
  87.  
  88.     for ( var p in o ) {
  89.         if ( typeof o[p] == 'string' ) {
  90.             str += levelStr +
  91.                 p + ': ' + o[p] + ' </br>';
  92.         } else {
  93.             str += levelStr +
  94.                 p + ': { </br>' + print( o[p], maxLevel, level + 1 ) + levelStr + '}</br>';
  95.         }
  96.     }
  97.  
  98.     // Remove this if you don't want the pre tag, but make sure to remove
  99.     // the open pre tag on the top as well
  100.     if ( level == 0 ) {
  101.         str += '</pre>';
  102.     }
  103.     return str;
  104. };
  105.      
  106. var pagewilsObject = {
  107.     name: 'Wilson Page',
  108.     contact: {
  109.         email: 'wilson@hotmail.com',
  110.         tel: '123456789'
  111.     }  
  112. }
  113.  
  114. // Recursive of whole object
  115. $('body').append( print(pagewilsObject) );
  116.  
  117. // Recursive of myObject up to 1 level, will only show name
  118. // and that there is a contact object
  119. $('body').append( print(pagewilsObject, 1) );
  120.      
  121. function ObjToSource(o){
  122.     if (!o) return 'null';
  123.     var k="",na=typeof(o.length)=="undefined"?1:0,str="";
  124.     for(var p in o){
  125.         if (na) k = "'"+p+ "':";
  126.         if (typeof o[p] == "string") str += k + "'" + o[p]+"',";
  127.         else if (typeof o[p] == "object") str += k + ObjToSource(o[p])+",";
  128.         else str += k + o[p] + ",";
  129.     }
  130.     if (na) return "{"+str.slice(0,-1)+"}";
  131.     else return "["+str.slice(0,-1)+"]";
  132. }
  133.      
  134. const ObjToSource=(o)=> {
  135.     if (!o) return null;
  136.     let str="",na=0,k,p;
  137.     if (typeof(o) == "object") {
  138.         if (!ObjToSource.check) ObjToSource.check = new Array();
  139.         for (k=ObjToSource.check.length;na<k;na++) if (ObjToSource.check[na]==o) return '{}';
  140.         ObjToSource.check.push(o);
  141.     }
  142.     k="",na=typeof(o.length)=="undefined"?1:0;
  143.     for(p in o){
  144.         if (na) k = "'"+p+"':";
  145.         if (typeof o[p] == "string") str += k+"'"+o[p]+"',";
  146.         else if (typeof o[p] == "object") str += k+ObjToSource(o[p])+",";
  147.         else str += k+o[p]+",";
  148.     }
  149.     if (typeof(o) == "object") ObjToSource.check.pop();
  150.     if (na) return "{"+str.slice(0,-1)+"}";
  151.     else return "["+str.slice(0,-1)+"]";
  152. }
  153.      
  154. var test1 = new Object();
  155. test1.foo = 1;
  156. test1.bar = 2;
  157.  
  158. var testobject = new Object();
  159. testobject.run = 1;
  160. testobject.fast = null;
  161. testobject.loop = testobject;
  162. testobject.dup = test1;
  163.  
  164. console.log(ObjToSource(testobject));
  165. console.log(testobject.toSource());
  166.      
  167. {'run':1,'fast':null,'loop':{},'dup':{'foo':1,'bar':2}}
  168. ({run:1, fast:null, loop:{run:1, fast:null, loop:{}, dup:{foo:1, bar:2}}, dup:{foo:1, bar:2}})
  169.      
  170. console.log(obj);
  171.      
  172. console.log("object is: %O", obj);
  173.      
  174. var list = function(object) {
  175.    for(var key in object) {
  176.      console.log(key);
  177.    }
  178. }
  179.      
  180. for (var i in obj){
  181.     console.log(obj[i], i);
  182. }
  183.      
  184. John 0
  185. Foo 1
  186. Bar 2
  187.      
  188. console.log("%o", obj);
  189.      
  190. var gandalf = {
  191.   "real name": "Gandalf",
  192.   "age (est)": 11000,
  193.   "race": "Maia",
  194.   "haveRetirementPlan": true,
  195.   "aliases": [
  196.     "Greyhame",
  197.     "Stormcrow",
  198.     "Mithrandir",
  199.     "Gandalf the Grey",
  200.     "Gandalf the White"
  201.   ]
  202. };
  203. //to console log object, we cannot use console.log("Object gandalf: " + gandalf);
  204. console.log("Object gandalf: ");
  205. //this will show object gandalf ONLY in Google Chrome NOT in IE
  206. console.log(gandalf);
  207. //this will show object gandalf IN ALL BROWSERS!
  208. console.log(JSON.stringify(gandalf));
  209. //this will show object gandalf IN ALL BROWSERS! with beautiful indent
  210. console.log(JSON.stringify(gandalf, null, 4));
  211.      
  212. console.log(Object.keys(yourObj));
  213. console.log(Object.values(yourObj));
  214.      
  215. Object.keys(yourObj).forEach(e => console.log(`key=${e}  value=${yourObj[e]}`));
  216.      
  217. console.table(yourObj)
  218.      
  219. console.log(yourObj)
  220.      
  221. <script type="text/javascript">
  222.     function print_r(theObj){
  223.        if(theObj.constructor == Array || theObj.constructor == Object){
  224.           document.write("<ul>")
  225.           for(var p in theObj){
  226.              if(theObj[p].constructor == Array || theObj[p].constructor == Object){
  227.                 document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
  228.                 document.write("<ul>")
  229.                 print_r(theObj[p]);
  230.                 document.write("</ul>")
  231.              } else {
  232.                 document.write("<li>["+p+"] => "+theObj[p]+"</li>");
  233.              }
  234.           }
  235.           document.write("</ul>")
  236.        }
  237.     }
  238. </script>
  239.      
  240. <script type="text/javascript">
  241. print_r(JAVACRIPT_ARRAY_OR_OBJECT);
  242. </script>
  243.      
  244. JSON.stringify(obj)
  245.      
  246. var args_string = JSON.stringify(obj);
  247. console.log(args_string);
  248.      
  249. alert(args_string);
  250.      
  251. foo.moo = "stackoverflow";
  252. console.log(foo.moo);
  253.      
  254. /**
  255.  * @param variable mixed  The var to log to the console
  256.  * @param varName string  Optional, will appear as a label before the var
  257.  */
  258. function dd(variable, varName) {
  259.     var varNameOutput;
  260.  
  261.     varName = varName || '';
  262.     varNameOutput = varName ? varName + ':' : '';
  263.  
  264.     console.warn(varNameOutput, variable, ' (' + (typeof variable) + ')');
  265. }
  266.      
  267. var obj = {field1: 'xyz', field2: 2016};
  268. dd(obj, 'My Cool Obj');
  269.      
  270. var print = function(obj, delp, delo, ind){
  271.     delp = delp!=null ? delp : "t"; // property delimeter
  272.     delo = delo!=null ? delo : "n"; // object delimeter
  273.     ind = ind!=null ? ind : " "; // indent; ind+ind geometric addition not great for deep objects
  274.     var str='';
  275.  
  276.     for(var prop in obj){
  277.         if(typeof obj[prop] == 'string' || typeof obj[prop] == 'number'){
  278.           var q = typeof obj[prop] == 'string' ? "" : ""; // make this "'" to quote strings
  279.           str += ind + prop + ': ' + q + obj[prop] + q + '; ' + delp;
  280.         }else{
  281.           str += ind + prop + ': {'+ delp + print(obj[prop],delp,delo,ind+ind) + ind + '}' + delo;
  282.         }
  283.     }
  284.     return str;
  285. };
  286.      
  287. var print = function( o, maxLevel, level )
  288. {
  289.     if ( typeof level == "undefined" )
  290.     {
  291.         level = 0;
  292.     }
  293.     if ( typeof maxlevel == "undefined" )
  294.     {
  295.         maxLevel = 0;
  296.     }
  297.  
  298.     var str = '';
  299.     // Remove this if you don't want the pre tag, but make sure to remove
  300.     // the close pre tag on the bottom as well
  301.     if ( level == 0 )
  302.     {
  303.         str = '<pre>';   // can also be <pre>
  304.     }
  305.  
  306.     var levelStr = '<br>';
  307.     for ( var x = 0; x < level; x++ )
  308.     {
  309.         levelStr += '    ';   // all those spaces only work with <pre>
  310.     }
  311.  
  312.     if ( maxLevel != 0 && level >= maxLevel )
  313.     {
  314.         str += levelStr + '...<br>';
  315.         return str;
  316.     }
  317.  
  318.     for ( var p in o )
  319.     {
  320.         switch(typeof o[p])
  321.         {
  322.           case 'string':
  323.           case 'number':    // .tostring() gets automatically applied
  324.           case 'boolean':   // ditto
  325.             str += levelStr + p + ': ' + o[p] + ' <br>';
  326.             break;
  327.  
  328.           case 'object':    // this is where we become recursive
  329.           default:
  330.             str += levelStr + p + ': [ <br>' + print( o[p], maxLevel, level + 1 ) + levelStr + ']</br>';
  331.             break;
  332.         }
  333.     }
  334.  
  335.     // Remove this if you don't want the pre tag, but make sure to remove
  336.     // the open pre tag on the top as well
  337.     if ( level == 0 )
  338.     {
  339.         str += '</pre>';   // also can be </pre>
  340.     }
  341.     return str;
  342. };
  343.      
  344. function printObj(obj) {
  345. console.log((function traverse(tab, obj) {
  346.     let str = "";
  347.     if(typeof obj !== 'object') {
  348.         return obj + ',';
  349.     }
  350.     if(Array.isArray(obj)) {            
  351.         return '[' + obj.map(o=>JSON.stringify(o)).join(',') + ']' + ',';
  352.     }
  353.     str = str + '{n';
  354.     for(var p in obj) {
  355.         str = str + tab + ' ' + p + ' : ' + traverse(tab+' ', obj[p]) +'n';
  356.     }
  357.     str = str.slice(0,-2) + str.slice(-1);                
  358.     str = str + tab + '},';
  359.     return str;
  360. }('',obj).slice(0,-1)))};
  361.      
  362. var result = beautinator({ "font-size": "26px","font-family": "'Open Sans', sans-serif",color: "white", overflow: "hidden",padding: "4px 4px 4px 8px",Text: { display: "block", width: "100%","text-align": "center", "padding-left": "2px","word-break": "break-word"}})
  363. console.log(result)
  364.      
  365. { "font-size": "26px",
  366.   "font-family": "'Open Sans', sans-serif",
  367.   color: "white", overflow: "hidden",
  368.   padding: "4px 4px 4px 8px",
  369.   Text: { display: "block", width: "100%",
  370.           "text-align": "center", "padding-left": "2px",
  371.           "word-break": "break-word"
  372.   }
  373. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top