Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Returns a complete human-readable breakdown of a Javascript object
/**
* debug_object (1.0.0)
* Returns a complete human-readable breakdown of a Javascript object
* Author: Matheus Pratta <https://github.com/matheusmk3>
*/
function debug_object (obj, braces, level) {
// Recursion level
if (!level) level = braces ? 1 : 0;
// Identation
var identation = '';
for (var i = 0; i < level; i++) {
identation += '\t';
}
// Output variable
var out = '';
var hasProps = false;
// Open
if (braces) out += '{';
for (var key in obj) {
// We have props
hasProps = true;
// Our current key value
var value = obj[key];
// Check if we're dealing with an object
if ( (typeof value === "object") && (value !== null) ) {
// Print object
out += '\n' + identation + key + ': ' + debug_object(value, braces, level + 1);
} else {
out += '\n' + identation + key + ': ' + JSON.stringify(value);
}
}
// Close
if (braces) out += ' }';
// Return processed output
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.