以某种方式,我构造if / else语句的方式导致此代码仅在page_type_id / panel_type_ids相同时才起作用。
对于以下代码段,首先可以正常工作,看到page_type_id为2,它使用该逻辑来隐藏不必要的div,然后使用其他语句来确定panel_type_id 2的内容为一个div,panel_type_id 3的内容为另一个。
如果所有元素都具有相同的page_type_id,则可以正常工作,但如果它从page_type_id 2变为1或3,则迭代会以某种方式中断,而我不知道该怎么做。
下面的代码段在每次迭代时应打印:
left 93 right 93
Full Page
Full Page 2
Full Page 3
这是代码段
const obj = [{
"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": "1",
"display_id": "2",
"slide_order": null,
"duration": "74",
"background_img": "images\/bg_rainbow.svg",
"panel_id": "87",
"panel_type_id": "1",
"cont_id": "139",
"contID": "139",
"content": "\r\n\r\n\r\n<\/head>\r\n\r\nFull Page<\/p>\r\n<\/body>\r\n<\/html>"
},
{
"pageID": "95",
"page_type_id": "1",
"display_id": "2",
"slide_order": null,
"duration": "74",
"background_img": "images\/bg_rainbow.svg",
"panel_id": "87",
"panel_type_id": "1",
"cont_id": "139",
"contID": "139",
"content": "\r\n\r\n\r\n<\/head>\r\n\r\nFull Page 2<\/p>\r\n<\/body>\r\n<\/html>"
},
{
"pageID": "96",
"page_type_id": "1",
"display_id": "2",
"slide_order": null,
"duration": "74",
"background_img": "images\/bg_rainbow.svg",
"panel_id": "87",
"panel_type_id": "1",
"cont_id": "139",
"contID": "139",
"content": "\r\n\r\n\r\n<\/head>\r\n\r\nFull Page 3<\/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 topLeftContent = document.getElementById('topLeftContent');
var topRightContent = document.getElementById('topRightContent');
var bottomLeftContent = document.getElementById('bottomLeftContent');
var bottomRightContent = document.getElementById('bottomRightContent');
var fullColumn = document.getElementById('fullColumn');
var leftColumn = document.getElementById('leftColumn');
var rightColumn = document.getElementById('rightColumn');
var leftColumnQtr = document.getElementById('leftColumnQtr');
var rightColumnQtrHalf = document.getElementById('rightColumnQtrHalf');
var rightColumnQtr = document.getElementById('rightColumnQtr');
var leftColumnQtrHalf = document.getElementById('leftColumnQtrHalf');
const pages_array = obj.reduce(function (pages_array, item, index, obj) {
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;
}, []);
setInterval(() => {
const currentJSONobject = pages_array[counter];
fullContent.innerHTML = '';
rightContent.innerHTML = '';
leftContent.innerHTML = '';
topLeftContent.innerHTML = '';
topRightContent.innerHTML = '';
bottomLeftContent.innerHTML = '';
bottomRightContent.innerHTML = '';
for (var i = 0; i < currentJSONobject.content.length; i++) {
if (currentJSONobject.content[i].page_type_id == 1) {
leftColumn.style.display = "none";
rightColumn.style.display = "none";
leftColumnQtr.style.display = "none";
rightColumnQtrHalf.style.display = "none";
rightColumnQtr.style.display = "none";
leftColumnQtrHalf.style.display = "none";
if (currentJSONobject.content[i].panel_type_id == 1) {
fullContent.innerHTML = currentJSONobject.content[i].content;
}
} else if (currentJSONobject.content[i].page_type_id == 2) {
fullColumn.style.display = "none";
leftColumnQtr.style.display = "none";
rightColumnQtrHalf.style.display = "none";
rightColumnQtr.style.display = "none";
leftColumnQtrHalf.style.display = "none";
if (currentJSONobject.content[i].panel_type_id == 2) {
leftContent.innerHTML = currentJSONobject.content[i].content;
}
if (currentJSONobject.content[i].panel_type_id == 3) {
rightContent.innerHTML = currentJSONobject.content[i].content;
}
}
}
console.log(pages_array[counter])
counter += 1;
if (counter === pages_array.length) {
counter = 0;
}
}, 1500)
console.log(obj);
<div class="row middle" id="middle" style="background-image: url();">
<!-- Full Page Divs -->
<div class="col-lg-12" id="fullColumn">
<div class="fullContent" id="fullContent" style="height: 100%; ">
</div>
</div>
<!-- End Full Page Divs -->
<!-- Half Page Divs -->
<div class="col-lg-6 leftColumn" id="leftColumn">
<div class="leftContent" id="leftContent" style=" height: 100%; ">
</div>
</div>
<div class="col-lg-6 rightColumn" id="rightColumn">
<div class="rightContent" id="rightContent" style=" height: 100%; ">
</div>
</div>
<!-- End Half Page Divs -->
</div>
<!-- End Row Middle -->
答案 0 :(得分:1)
为了简化代码,为什么不在函数的for loop
中提取逻辑,而在函数内部使用switch
而不是疯狂的if/else if/else
。
for(var i = 0; i < currentJSONobject.content.length; i++){
myFn(currentJSONobject, index)
}
function myFn(jsonObj, index) {
switch (jsonObj.content[index].page_type_id ) {
case 1:
//...<logic for id 1>
break;
case 2:
//...<logic for id 2>
break:
default:
//... etc
}
}
一旦简化了代码,可能会更容易确定问题所在。