简体   繁体   English

访问json对象循环的嵌套/内部循环

[英]Accessing nested/inner loop of json object loop

I've finally refactored my code so that the console prints the correct arrays, but I can't figure out how to access the nested values so that the 'content' value prints in the HTML divs. 我终于重构了代码,以便控制台打印正确的数组,但是我不知道如何访问嵌套值,以便在HTML div中打印“内容”值。

In the snippet/example below, the counter moves to each object element like I expect, cycling by pageID (use the console to see this in the fiddle). 在下面的代码片段/示例中,计数器按我的预期移动到每个对象元素,并按pageID循环(使用控制台在小提琴中查看)。

That works fine but the point of this is to show the value in the 'content' field of the object array in the correct DIVs. 效果很好,但是这样做的目的是在正确的DIV中显示对象数组的“ content”字段中的值。 My logic to put certain content into the divs works, but I can't figure out how to actually access the content values in the object array now that it's nested in the loop. 我将某些内容放入div的逻辑有效,但由于嵌套在循环中,所以我不知道如何实际访问对象数组中的内容值。

So for this snippet, when console shows the object array for pageID 93, the divs should respectively show 'Left 93' and 'Right 93'. 因此,对于此代码段,当控制台显示pageID 93的对象数组时,div应该分别显示“ Left 93”和“ Right 93”。 When the console moves on to pageID 94, one of the divs should show 'Page 94' and so on. 当控制台移至pageID 94时,div之一应显示'Page 94',依此类推。 The console increments correctly, and my logic for the divs is correct, but any guidance on how to access the internal 'Content' array is much much apprecaited 控制台正确增加,并且我的div逻辑正确,但是关于如何访问内部“ Content”数组的任何指导都非常重要

If you need fiddle: http://jsfiddle.net/gq0t4j93/4/ 如果您需要提琴: http : //jsfiddle.net/gq0t4j93/4/

 const original_json = [{ "pageID": "93", "page_type_id": "2", "display_id": "2", "slide_order": null, "duration": "74", "background_img": "images\\/bg_rainbow.svg", "panel_id": "86", "panel_type_id": "2", "cont_id": "138", "contID": "138", "content": "\\r\\n\\r\\n\\r\\n<\\/head>\\r\\n\\r\\nLeft 93<\\/p>\\r\\n<\\/body>\\r\\n<\\/html>" }, { "pageID": "93", "page_type_id": "2", "display_id": "2", "slide_order": null, "duration": "74", "background_img": "images\\/bg_rainbow.svg", "panel_id": "87", "panel_type_id": "3", "cont_id": "139", "contID": "139", "content": "\\r\\n\\r\\n\\r\\n<\\/head>\\r\\n\\r\\nRight 93<\\/p>\\r\\n<\\/body>\\r\\n<\\/html>" }, { "pageID": "94", "page_type_id": "2", "display_id": "2", "slide_order": null, "duration": "74", "background_img": "images\\/bg_rainbow.svg", "panel_id": "87", "panel_type_id": "3", "cont_id": "139", "contID": "139", "content": "\\r\\n\\r\\n\\r\\n<\\/head>\\r\\n\\r\\nPage 94<\\/p>\\r\\n<\\/body>\\r\\n<\\/html>" }, { "pageID": "95", "page_type_id": "2", "display_id": "2", "slide_order": null, "duration": "74", "background_img": "images\\/bg_rainbow.svg", "panel_id": "87", "panel_type_id": "3", "cont_id": "139", "contID": "139", "content": "\\r\\n\\r\\n\\r\\n<\\/head>\\r\\n\\r\\nPage 95<\\/p>\\r\\n<\\/body>\\r\\n<\\/html>" }, { "pageID": "96", "page_type_id": "2", "display_id": "2", "slide_order": null, "duration": "74", "background_img": "images\\/bg_rainbow.svg", "panel_id": "87", "panel_type_id": "3", "cont_id": "139", "contID": "139", "content": "\\r\\n\\r\\n\\r\\n<\\/head>\\r\\n\\r\\nPage 96<\\/p>\\r\\n<\\/body>\\r\\n<\\/html>" }, ]; let counter = 0; var fullContent = document.getElementById('fullContent'); var leftContent = document.getElementById('leftContent'); var rightContent = document.getElementById('rightContent'); var fullColumn = document.getElementById('fullColumn'); var leftColumn = document.getElementById('leftColumn'); var rightColumn = document.getElementById('rightColumn'); // loop through original json // for each item, get page ID and see if we've already created a new Page object for it // if we have, add the object from the original json to the "content" array of the new page object // otherwise, create a new Page object to put in our new array const pages_array = original_json.reduce(function(pages_array, item, index, original_json) { const current_pageID = item.pageID; const exisiting_page = pages_array.find(page => page.pageID === current_pageID); if (exisiting_page === undefined) { const new_Page = { pageID: current_pageID, content: [item] } pages_array.push(new_Page); } else { exisiting_page.content.push(item) } return pages_array; }, []); // Open console to see data console.clear(); //console.log(pages_array); //this prints correct array setInterval(() => { //here I loop through pages, but i need to loop within here over content to render html const currentJSONobject = pages_array[counter]; if (currentJSONobject.page_type_id == 2) { fullColumn.style.display = "none"; if (currentJSONobject.panel_type_id == 2) { leftContent.innerHTML = currentJSONobject.content; } else if (currentJSONobject.panel_type_id == 3) { rightContent.innerHTML = currentJSONobject.content; } } console.log(pages_array[counter]) counter += 1; if (counter === pages_array.length) { counter = 0; } }, 1500) 
 <div class="row middle" id="middle" style="background-image: url();"> <!-- Half Page Divs --> <div class="col-lg-6 leftColumn"> <div class="leftContent" id="leftContent" style=" height: 100%; "> </div> </div> <div class="col-lg-6 rightColumn"> <div class="rightContent" id="rightContent" style=" height: 100%; "> </div> </div> <!-- End Half Page Divs --> </div> <!-- End Row Middle --> 

I'm not sure if I get your needs, but as I understand it here's what you want to do: 我不确定是否能满足您的需求,但是据我了解,这是您要执行的操作:

 const oj = [{ "pageID": "93", "page_type_id": "2", "display_id": "2", "slide_order": null, "duration": "74", "background_img": "images\\/bg_rainbow.svg", "panel_id": "86", "panel_type_id": "2", "cont_id": "138", "contID": "138", "content": "\\r\\n\\r\\n\\r\\n<\\/head>\\r\\n\\r\\nLeft 93<\\/p>\\r\\n<\\/body>\\r\\n<\\/html>" }, { "pageID": "93", "page_type_id": "2", "display_id": "2", "slide_order": null, "duration": "74", "background_img": "images\\/bg_rainbow.svg", "panel_id": "87", "panel_type_id": "3", "cont_id": "139", "contID": "139", "content": "\\r\\n\\r\\n\\r\\n<\\/head>\\r\\n\\r\\nRight 93<\\/p>\\r\\n<\\/body>\\r\\n<\\/html>" }, { "pageID": "94", "page_type_id": "2", "display_id": "2", "slide_order": null, "duration": "74", "background_img": "images\\/bg_rainbow.svg", "panel_id": "87", "panel_type_id": "3", "cont_id": "139", "contID": "139", "content": "\\r\\n\\r\\n\\r\\n<\\/head>\\r\\n\\r\\nPage 94<\\/p>\\r\\n<\\/body>\\r\\n<\\/html>" }, { "pageID": "95", "page_type_id": "2", "display_id": "2", "slide_order": null, "duration": "74", "background_img": "images\\/bg_rainbow.svg", "panel_id": "87", "panel_type_id": "3", "cont_id": "139", "contID": "139", "content": "\\r\\n\\r\\n\\r\\n<\\/head>\\r\\n\\r\\nPage 95<\\/p>\\r\\n<\\/body>\\r\\n<\\/html>" }, { "pageID": "96", "page_type_id": "2", "display_id": "2", "slide_order": null, "duration": "74", "background_img": "images\\/bg_rainbow.svg", "panel_id": "87", "panel_type_id": "3", "cont_id": "139", "contID": "139", "content": "\\r\\n\\r\\n\\r\\n<\\/head>\\r\\n\\r\\nPage 96<\\/p>\\r\\n<\\/body>\\r\\n<\\/html>" }, ]; // let's say you want to access pageID 93 and you have that in a variable let pId = "93"; // first filter your original array down to the entries matching pId console.log(oj.filter( i => i.pageID === pId ) // then map the resulting array to only contain the content .map(i => i.content)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM