I can not post the correct result. There is a JSON file and you need to output this using cycles:

<div class="inner"> <span class="f_text">this 1</span> <span class="f_pos">мСстоимСниС 1</span> <span class="f_ts">Γ°Ιͺs 1</span> <span class="l_text">этот 2</span> <span class="l_pos">мСстоимСниС 2</span> </div> <div class="inner"> <span class="f_text">this 1</span> <span class="f_pos">частица 1</span> <span class="f_ts">Γ°Ιͺs 1</span> <span class="l_text">это 2</span> <span class="l_pos">частица 2</span> </div> <div> <span class="f_text">this 1</span> <span class="f_pos">ΠΏΡ€ΠΈΠ»Π°Π³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ 1</span> <span class="f_ts">Γ°Ιͺs 1</span> <span class="l_text">настоящий 2</span> <span class="l_pos">ΠΏΡ€ΠΈΠ»Π°Π³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ 2</span> </div> 

JSON file:

 { "head": {}, "def": [{ "text": "this", "pos": "мСстоимСниС", "ts": "Γ°Ιͺs", "tr": [{ "text": "этот", "pos": "мСстоимСниС", "syn": [{ "text": "Ρ‚Π°ΠΊΠΎΠΉ", "pos": "мСстоимСниС" }, { "text": "сСй", "pos": "мСстоимСниС" }], "mean": [{ "text": "the" }, { "text": "still" }] }] }, { "text": "this", "pos": "частица", "ts": "Γ°Ιͺs", "tr": [{ "text": "это", "pos": "частица" }] }, { "text": "this", "pos": "ΠΏΡ€ΠΈΠ»Π°Π³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅", "ts": "Γ°Ιͺs", "tr": [{ "text": "настоящий", "pos": "ΠΏΡ€ΠΈΠ»Π°Π³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅", "syn": [{ "text": "ΠΏΠΎΠ΄ΠΎΠ±Π½Ρ‹ΠΉ", "pos": "ΠΏΡ€ΠΈΠ»Π°Π³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅" }, { "text": "Π½Ρ‹Π½Π΅ΡˆΠ½ΠΈΠΉ", "pos": "ΠΏΡ€ΠΈΠ»Π°Π³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅" }], "mean": [{ "text": "present" }, { "text": "such" }, { "text": "current" }], "ex": [{ "text": "this button", "tr": [{ "text": "настоящая ΠΊΠ½ΠΎΠΏΠΊΠ°" }] }] }] }] } 

My javascript code is:

 var output = ""; var main_div = document.createElement("div"); main_div.setAttribute('id', "main"); document.body.appendChild(main_div); for (var i = 0; i < info["def"].length; i++) { var inner_div = document.createElement('div'); inner_div.className = "inner"; var res = main_div.appendChild(inner_div); for (var j = 0; j < info["def"][i]["tr"].length; j++) { for (var key in info["def"][i]) { if (info["def"][i].hasOwnProperty(key) && typeof info["def"][i][key] != "object") { output += '<span class=f_' + key + '>' + info["def"][i][key] + " " + ' 1</span>'; } } for (var key2 in info["def"][i]["tr"][j]) { if (info["def"][i]["tr"][j].hasOwnProperty(key2) && typeof info["def"][i]["tr"][j][key2] != "object") { output += '<span class=l_' + key2 + '>' + info["def"][i]["tr"][j][key2] + " " + ' 2</span>'; } } } //for each object res.insertAdjacentHTML('afterBegin', output); } 

    1 answer 1

    Your mistake is that after adding the markup, the output is not cleared, so the necessary elements are inserted for the first time, the necessary elements for the second element are added to the second iteration, and the second element is inserted into the second element and for the first and for the second.

    A simple solution would be to simply initialize the output an empty string at the beginning of the main loop.

     var info = { "head": {}, "def": [{ "text": "this", "pos": "мСстоимСниС", "ts": "Γ°Ιͺs", "tr": [{ "text": "этот", "pos": "мСстоимСниС", "syn": [{ "text": "Ρ‚Π°ΠΊΠΎΠΉ", "pos": "мСстоимСниС" }, { "text": "сСй", "pos": "мСстоимСниС" }], "mean": [{ "text": "the" }, { "text": "still" }] }] }, { "text": "this", "pos": "частица", "ts": "Γ°Ιͺs", "tr": [{ "text": "это", "pos": "частица" }] }, { "text": "this", "pos": "ΠΏΡ€ΠΈΠ»Π°Π³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅", "ts": "Γ°Ιͺs", "tr": [{ "text": "настоящий", "pos": "ΠΏΡ€ΠΈΠ»Π°Π³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅", "syn": [{ "text": "ΠΏΠΎΠ΄ΠΎΠ±Π½Ρ‹ΠΉ", "pos": "ΠΏΡ€ΠΈΠ»Π°Π³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅" }, { "text": "Π½Ρ‹Π½Π΅ΡˆΠ½ΠΈΠΉ", "pos": "ΠΏΡ€ΠΈΠ»Π°Π³Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠ΅" }], "mean": [{ "text": "present" }, { "text": "such" }, { "text": "current" }], "ex": [{ "text": "this button", "tr": [{ "text": "настоящая ΠΊΠ½ΠΎΠΏΠΊΠ°" }] }] }] }] }; var output = ""; var main_div = document.createElement("div"); main_div.setAttribute('id', "main"); document.body.appendChild(main_div); for (var i = 0; i < info["def"].length; i++) { output = ""; var inner_div = document.createElement('div'); inner_div.className = "inner"; var res = main_div.appendChild(inner_div); for (var j = 0; j < info["def"][i]["tr"].length; j++) { for (var key in info["def"][i]) { if (info["def"][i].hasOwnProperty(key) && typeof info["def"][i][key] != "object") { output += '<span class=f_' + key + '>' + info["def"][i][key] + " " + ' 1</span>'; } } for (var key2 in info["def"][i]["tr"][j]) { if (info["def"][i]["tr"][j].hasOwnProperty(key2) && typeof info["def"][i]["tr"][j][key2] != "object") { output += '<span class=l_' + key2 + '>' + info["def"][i]["tr"][j][key2] + " " + ' 2</span>'; } } } //for each object res.insertAdjacentHTML('afterBegin', output); } 
     .inner { border: 1px solid black; }