Object Iterator
Iterates recursively through a javascript Object and shows enclosed Objects indented. Basic, could be used to build a tree or something.
by KooiInc MeHere
HTML
<div id="result"></div>
CSS
body {
font: normal 12px/15px verdana, arial;
margin: 3rem;
}
JavaScript
function objectIterator(obj) {
var result = [],indent = '';
function getProps(obj, doIndent){
var level = 0;
for (var property in obj) {
if (obj.hasOwnProperty(property)){
if (obj[property].constructor === Object) {
result.push(indent+'+['+property+']: Object'+' (level '
+((indent.length/12)+1)+')');
indent += ' '
getProps(obj[property],true);
indent = '';
} else {
result.push((doIndent ? (indent || '') : '') + property + ": "
+ obj[property]);
}
}
}
}
getProps(obj); //tail optimizing
return result;
}
var myobj = {
a: 1,
b: [1,2,3,4,5,6,7],
c: { aa:1,
bb:2,
cc:3
},
d: { xyz:'yeps',
other:{ another:1,
yetanotherotherone: {q:1,w:2,z:'sure',onemore:{om1:1,om2:2}},
end:2}
},
e: 15,
f: function(){return 'bla';}
};
$('#result').html(objectIterator(myobj).join('<br />'));