I have an object:
var data = {
"lay": "h_m_f",
"h": {
"par": {
"first_second": {
"first": {
"module": {
"article": {
"archivclankyrubrika": "3",
"pocetclanku": "10"
}
}
},
"second": {
}
}
},
"next": {
"one": {
"val": {
}
},
}
},
}
};
I try to build html nested divs from this object. And if node doesn't has children then show inner text. But problem is that this script doesn't loop through children. In result I have only 2 divs.
Where is my problem guys? Thanks for answer. This is my script:
function create_node(obj){
var node = document.createElement('div');
node.setAttribute('class', Object.keys(obj)[0]);
for (var i in obj) {
node.appendChild(create_node(obj.i));
}
return node;
}
console.log(create_node(jsonTemplate));
Update: ideal result for me: every object is div with class before module. Module is the last div and cihldren's key of module show as a text.
<div class="h">
<div class="par">
<div class='first_second'>
<div class="first">
<div class="module">
article
</div>
</div>
<div class="second">
</div>
</div>
</div>
<div class="next">
<div class="one">
<div class="val">
</div>
</div>
</div>
</div>
obj.iis not correct, it should beobj[i]<div class="h"><div class="first_second"><div class="module">article</div></div></div>parandfirst,archivclankyrubrika,3?