gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:20:29 24 4
gpt4 key购买 nike

我终于重构了我的代码,以便控制台打印正确的数组,但我不知道如何访问嵌套值,以便在 HTML div 中打印“内容”值。

在下面的代码片段/示例中,计数器按照我的预期移动到每个对象元素,按 pageID 循环(使用控制台在 fiddle 中查看这一点)。

这工作正常,但重点是在正确的 DIV 中显示对象数组的“内容”字段中的值。我将某些内容放入 div 的逻辑是有效的,但我不知道如何实际访问对象数组中的内容值,因为它嵌套在循环中。

因此,对于此代码片段,当控制台显示 pageID 93 的对象数组时,div 应分别显示“Left 93”和“Right 93”。当控制台移至 pageID 94 时,其中一个 div 应显示“Page 94”,依此类推。控制台正确递增,我的 div 逻辑是正确的,但是任何有关如何访问内部“内容”数组的指导都非常值得赞赏

如果您需要 fiddle :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 -->

最佳答案

我不确定我是否满足您的需求,但据我了解,这就是您想要做的:

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))

关于javascript - 访问 json 对象循环的嵌套/内部循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51788142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com