Skip to content

Instantly share code, notes, and snippets.

@joshuatz
Created November 27, 2017 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuatz/23c06e9863e1e208eca810b1981f8336 to your computer and use it in GitHub Desktop.
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
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