Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- console.log(obj)
- var obj = {prop1: 'prop1Value', prop2: 'prop2Value', child: {childProp1: 'childProp1Value'}}
- console.log(obj)
- console.log('My object : ' + obj)
- str = JSON.stringify(obj);
- str = JSON.stringify(obj, null, 4); // (Optional) beautiful indented output.
- console.log(str); // Logs output to dev tools console.
- alert(str); // Displays output using window.alert()
- obj = JSON.parse(str); // Reverses above operation (Just in case if needed.)
- "Uncaught TypeError: Converting circular structure to JSON"
- var output = '';
- for (var property in object) {
- output += property + ': ' + object[property]+'; ';
- }
- alert(output);
- console.log(JSON.stringify(obj))
- var print = function(o){
- var str='';
- for(var p in o){
- if(typeof o[p] == 'string'){
- str+= p + ': ' + o[p]+'; </br>';
- }else{
- str+= p + ': { </br>' + print(o[p]) + '}';
- }
- }
- return str;
- }
- var myObject = {
- name: 'Wilson Page',
- contact: {
- email: 'wilson@hotmail.com',
- tel: '123456789'
- }
- }
- $('body').append( print(myObject) );
- console.table(obj);
- console.table(obj, ['firstName', 'lastName']);
- var getPrintObject=function(object)
- {
- return JSON.stringify(object);
- }
- console.log('print object: ' + JSON.stringify(session));
- console.dir(object, {depth: null, colors: true})
- // Recursive print of object
- var print = function( o, maxLevel, level ) {
- if ( typeof level == "undefined" ) {
- level = 0;
- }
- if ( typeof level == "undefined" ) {
- maxLevel = 0;
- }
- var str = '';
- // Remove this if you don't want the pre tag, but make sure to remove
- // the close pre tag on the bottom as well
- if ( level == 0 ) {
- str = '<pre>';
- }
- var levelStr = '';
- for ( var x = 0; x < level; x++ ) {
- levelStr += ' ';
- }
- if ( maxLevel != 0 && level >= maxLevel ) {
- str += levelStr + '...</br>';
- return str;
- }
- for ( var p in o ) {
- if ( typeof o[p] == 'string' ) {
- str += levelStr +
- p + ': ' + o[p] + ' </br>';
- } else {
- str += levelStr +
- p + ': { </br>' + print( o[p], maxLevel, level + 1 ) + levelStr + '}</br>';
- }
- }
- // Remove this if you don't want the pre tag, but make sure to remove
- // the open pre tag on the top as well
- if ( level == 0 ) {
- str += '</pre>';
- }
- return str;
- };
- var pagewilsObject = {
- name: 'Wilson Page',
- contact: {
- email: 'wilson@hotmail.com',
- tel: '123456789'
- }
- }
- // Recursive of whole object
- $('body').append( print(pagewilsObject) );
- // Recursive of myObject up to 1 level, will only show name
- // and that there is a contact object
- $('body').append( print(pagewilsObject, 1) );
- function ObjToSource(o){
- if (!o) return 'null';
- var k="",na=typeof(o.length)=="undefined"?1:0,str="";
- for(var p in o){
- if (na) k = "'"+p+ "':";
- if (typeof o[p] == "string") str += k + "'" + o[p]+"',";
- else if (typeof o[p] == "object") str += k + ObjToSource(o[p])+",";
- else str += k + o[p] + ",";
- }
- if (na) return "{"+str.slice(0,-1)+"}";
- else return "["+str.slice(0,-1)+"]";
- }
- const ObjToSource=(o)=> {
- if (!o) return null;
- let str="",na=0,k,p;
- if (typeof(o) == "object") {
- if (!ObjToSource.check) ObjToSource.check = new Array();
- for (k=ObjToSource.check.length;na<k;na++) if (ObjToSource.check[na]==o) return '{}';
- ObjToSource.check.push(o);
- }
- k="",na=typeof(o.length)=="undefined"?1:0;
- for(p in o){
- if (na) k = "'"+p+"':";
- if (typeof o[p] == "string") str += k+"'"+o[p]+"',";
- else if (typeof o[p] == "object") str += k+ObjToSource(o[p])+",";
- else str += k+o[p]+",";
- }
- if (typeof(o) == "object") ObjToSource.check.pop();
- if (na) return "{"+str.slice(0,-1)+"}";
- else return "["+str.slice(0,-1)+"]";
- }
- var test1 = new Object();
- test1.foo = 1;
- test1.bar = 2;
- var testobject = new Object();
- testobject.run = 1;
- testobject.fast = null;
- testobject.loop = testobject;
- testobject.dup = test1;
- console.log(ObjToSource(testobject));
- console.log(testobject.toSource());
- {'run':1,'fast':null,'loop':{},'dup':{'foo':1,'bar':2}}
- ({run:1, fast:null, loop:{run:1, fast:null, loop:{}, dup:{foo:1, bar:2}}, dup:{foo:1, bar:2}})
- console.log(obj);
- console.log("object is: %O", obj);
- var list = function(object) {
- for(var key in object) {
- console.log(key);
- }
- }
- for (var i in obj){
- console.log(obj[i], i);
- }
- John 0
- Foo 1
- Bar 2
- console.log("%o", obj);
- var gandalf = {
- "real name": "Gandalf",
- "age (est)": 11000,
- "race": "Maia",
- "haveRetirementPlan": true,
- "aliases": [
- "Greyhame",
- "Stormcrow",
- "Mithrandir",
- "Gandalf the Grey",
- "Gandalf the White"
- ]
- };
- //to console log object, we cannot use console.log("Object gandalf: " + gandalf);
- console.log("Object gandalf: ");
- //this will show object gandalf ONLY in Google Chrome NOT in IE
- console.log(gandalf);
- //this will show object gandalf IN ALL BROWSERS!
- console.log(JSON.stringify(gandalf));
- //this will show object gandalf IN ALL BROWSERS! with beautiful indent
- console.log(JSON.stringify(gandalf, null, 4));
- console.log(Object.keys(yourObj));
- console.log(Object.values(yourObj));
- Object.keys(yourObj).forEach(e => console.log(`key=${e} value=${yourObj[e]}`));
- console.table(yourObj)
- console.log(yourObj)
- <script type="text/javascript">
- 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>")
- }
- }
- </script>
- <script type="text/javascript">
- print_r(JAVACRIPT_ARRAY_OR_OBJECT);
- </script>
- JSON.stringify(obj)
- var args_string = JSON.stringify(obj);
- console.log(args_string);
- alert(args_string);
- foo.moo = "stackoverflow";
- console.log(foo.moo);
- /**
- * @param variable mixed The var to log to the console
- * @param varName string Optional, will appear as a label before the var
- */
- function dd(variable, varName) {
- var varNameOutput;
- varName = varName || '';
- varNameOutput = varName ? varName + ':' : '';
- console.warn(varNameOutput, variable, ' (' + (typeof variable) + ')');
- }
- var obj = {field1: 'xyz', field2: 2016};
- dd(obj, 'My Cool Obj');
- var print = function(obj, delp, delo, ind){
- delp = delp!=null ? delp : "t"; // property delimeter
- delo = delo!=null ? delo : "n"; // object delimeter
- ind = ind!=null ? ind : " "; // indent; ind+ind geometric addition not great for deep objects
- var str='';
- for(var prop in obj){
- if(typeof obj[prop] == 'string' || typeof obj[prop] == 'number'){
- var q = typeof obj[prop] == 'string' ? "" : ""; // make this "'" to quote strings
- str += ind + prop + ': ' + q + obj[prop] + q + '; ' + delp;
- }else{
- str += ind + prop + ': {'+ delp + print(obj[prop],delp,delo,ind+ind) + ind + '}' + delo;
- }
- }
- return str;
- };
- var print = function( o, maxLevel, level )
- {
- if ( typeof level == "undefined" )
- {
- level = 0;
- }
- if ( typeof maxlevel == "undefined" )
- {
- maxLevel = 0;
- }
- var str = '';
- // Remove this if you don't want the pre tag, but make sure to remove
- // the close pre tag on the bottom as well
- if ( level == 0 )
- {
- str = '<pre>'; // can also be <pre>
- }
- var levelStr = '<br>';
- for ( var x = 0; x < level; x++ )
- {
- levelStr += ' '; // all those spaces only work with <pre>
- }
- if ( maxLevel != 0 && level >= maxLevel )
- {
- str += levelStr + '...<br>';
- return str;
- }
- for ( var p in o )
- {
- switch(typeof o[p])
- {
- case 'string':
- case 'number': // .tostring() gets automatically applied
- case 'boolean': // ditto
- str += levelStr + p + ': ' + o[p] + ' <br>';
- break;
- case 'object': // this is where we become recursive
- default:
- str += levelStr + p + ': [ <br>' + print( o[p], maxLevel, level + 1 ) + levelStr + ']</br>';
- break;
- }
- }
- // Remove this if you don't want the pre tag, but make sure to remove
- // the open pre tag on the top as well
- if ( level == 0 )
- {
- str += '</pre>'; // also can be </pre>
- }
- return str;
- };
- function printObj(obj) {
- console.log((function traverse(tab, obj) {
- let str = "";
- if(typeof obj !== 'object') {
- return obj + ',';
- }
- if(Array.isArray(obj)) {
- return '[' + obj.map(o=>JSON.stringify(o)).join(',') + ']' + ',';
- }
- str = str + '{n';
- for(var p in obj) {
- str = str + tab + ' ' + p + ' : ' + traverse(tab+' ', obj[p]) +'n';
- }
- str = str.slice(0,-2) + str.slice(-1);
- str = str + tab + '},';
- return str;
- }('',obj).slice(0,-1)))};
- 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"}})
- console.log(result)
- { "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"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement