Recursing object
by dandoyon
HTML
<textarea id="TEST"></textarea>
CSS
textarea { height: 200px; width: 300px; }
JavaScript
var obj = { first: "John", last: "Doe", blah: { foo: 'bar', who: 'car'} };
var odump = (function(){
var max, INDENT = " ";
function valueToStr(value, depth) {
switch (typeof value) {
case "object": return objectToStr(value, depth + 1);
case "function": return "function";
default: return "";
}
}
function objectToStr(object, depth) {
if (depth > max)
return false;
var output = "";
for (var key in object) {
output += "\n" + INDENT.substr(0,2*depth) + key;
var val = valueToStr(object[key], depth);
if(val != '') {
output += '.' + val;
} else {
output += ',';
}
}
return output;
};
return function odump(object, depth, _max) {
max = _max || 10;
return objectToStr(object, depth || 0);
};
})();
$("#TEST").text(odump(obj));