Created
November 27, 2017 01:48
-
-
Save joshuatz/23c06e9863e1e208eca810b1981f8336 to your computer and use it in GitHub Desktop.
Spaghetti code to turn an object into a nice console.group() print out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function objectToConsoleGroup(myObject,OPT_Name){ | |
var outerName = (OPT_Name||"Results"); | |
var done = false; | |
function main(prop,val){ | |
if (typeof(val)==="object" && !Array.isArray(val)){ | |
var thingObj = val; | |
if (typeof(prop)!=="undefined" && prop!==null){ | |
console.group("Object: " + prop); | |
} | |
else { | |
console.group("Object ˅") | |
} | |
//console.group("Object: " + prop); | |
for (var prop in thingObj){ | |
main(prop,thingObj[prop]); | |
} | |
console.groupEnd(); | |
} | |
else if (typeof(val)==="string" || typeof(val)==="number" || typeof(val)==="boolean"){ | |
if (typeof(prop)!=="undefined" && prop!==null){ | |
console.log(prop + " = " + val); | |
} | |
else { | |
console.log(val); | |
} | |
} | |
else if (Array.isArray(val)){ | |
var thingArr = val; | |
if (typeof(prop)!=="undefined" && prop!==null){ | |
console.group("Array: " + prop); | |
} | |
else { | |
console.group("Array ˅") | |
} | |
//console.group("Array: " + prop); | |
for (var x=0; x<thingArr.length; x++){ | |
main(null,thingArr[x]); | |
} | |
console.groupEnd(); | |
} | |
else { | |
val = val.toString(); | |
if (typeof(prop)!=="undefined" && prop!==null){ | |
console.log(prop + " = " + val); | |
} | |
else { | |
console.log(val); | |
} | |
} | |
} | |
if (typeof(console.group)=="function"){ | |
console.group(outerName); | |
for (var prop in myObject){ | |
main(prop,myObject[prop]); | |
} | |
console.groupEnd(); | |
} | |
else { | |
console.log("Error: console.group() is unsupported on this browser!"); | |
} | |
return true; | |
} | |
// Sample | |
(function(){ | |
var testObj = { | |
"outerObj" : { | |
"innerProp" : "innerVal", | |
"blahblah" : "blahVal" | |
}, | |
"numberTest" : 20, | |
"simpleString" : "asdf", | |
"arrayTest" : [ | |
"arrayStringVal", | |
123, | |
{"arrayObjProp" : "arrayObjVal"} | |
], | |
"testBoolean" : false, | |
"testFunction" : function(){return true} | |
} | |
objectToConsoleGroup(testObj); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment