Last active
February 2, 2016 03:24
-
-
Save RichardHan/ce333b16e98dba24b7ba to your computer and use it in GitHub Desktop.
JavaScript dump function
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
//http://www.openjs.com/scripts/others/dump_function_php_print_r.php | |
//http://stackoverflow.com/questions/749266/object-dump-javascript | |
(function() { | |
console.dump1 = function(object) { | |
if (window.JSON && window.JSON.stringify) | |
console.log(JSON.stringify(object)); | |
else | |
console.log(object); | |
}; | |
console.dump2 = function mydump(arr,level) { | |
var dumped_text = ""; | |
if(!level) level = 0; | |
var level_padding = ""; | |
for(var j=0;j<level+1;j++) level_padding += " "; | |
if(typeof(arr) == 'object') { | |
for(var item in arr) { | |
var value = arr[item]; | |
if(typeof(value) == 'object') { | |
dumped_text += level_padding + "'" + item + "' ...\n"; | |
dumped_text += mydump(value,level+1); | |
} else { | |
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n"; | |
} | |
} | |
} else { | |
dumped_text = "===>"+arr+"<===("+typeof(arr)+")"; | |
} | |
return dumped_text; | |
} | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment