【问题标题】:Accessing multiple array elements for JS if/else访问 JS if/else 的多个数组元素
【发布时间】:2018-08-15 13:10:34
【问题描述】:

我有一个小提琴http://jsfiddle.net/gq0t4j93/51/ 显示我在尝试访问不同级别的数组时遇到问题的页面。

我使用简单的三元运算进行了此操作,但我新添加的 if/else 语句现在什么也没有显示。

如果你查看小提琴的 JS,你会看到我有一个三元运算符的注释部分

    //   fullContent.innerHTML = '';
//   rightContent.innerHTML = '';
//   leftContent.innerHTML = '';

// fullContent.innerHTML = currentJSONobject.content[i].panel_type_id == 1 ? currentJSONobject.content[i].content : fullContent.innerHTML;

// leftContent.innerHTML = currentJSONobject.content[i].panel_type_id == 2 ? currentJSONobject.content[i].content : leftContent.innerHTML;

// rightContent.innerHTML = currentJSONobject.content[i].panel_type_id == 3 ? currentJSONobject.content[i].content : rightContent.innerHTML;

这是一个只使用三元运算符http://jsfiddle.net/gq0t4j93/55/的小提琴

当这些都到位并且我删除它上面的 if/else 部分时,它会按预期工作。但我需要使用 if else 语句来决定将哪些内容放入哪个 div,具体取决于页面和面板类型。

我正在尝试将其设置为“如果顶级 (page_id) 数组的 page_type_id 为 n,则检查该页面的每个内容元素的面板类型。如果为 n,则放入此 div,如果 x放入那个div"

我真正需要弄清楚的是如何正确访问它,以便在每次迭代时,我检查 page_type_id 以便我知道要隐藏哪些 DIVS,然后检查 panel_type_id 以便我知道将哪些内容放入哪个 div。

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": "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 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];
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;
            }else if(currentJSONobject.content[i].panel_type_id == 3){
                rightContent.innerHTML = currentJSONobject.content[i].content;
            }
        }


    //   fullContent.innerHTML = '';
    //   rightContent.innerHTML = '';
    //   leftContent.innerHTML = '';

    // fullContent.innerHTML = currentJSONobject.content[i].panel_type_id == 1 ? currentJSONobject.content[i].content : fullContent.innerHTML;
    
    // leftContent.innerHTML = currentJSONobject.content[i].panel_type_id == 2 ? currentJSONobject.content[i].content : leftContent.innerHTML;
    
    // rightContent.innerHTML = currentJSONobject.content[i].panel_type_id == 3 ? currentJSONobject.content[i].content : rightContent.innerHTML;

    }


  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();">
  
  <div class="col-lg-6 fullColumn">

    <div class="fullContent" id="fullContent" style=" height: 100%; ">

    </div>
  </div>

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

【问题讨论】:

  • 最好将所有相关代码发布在这里,而不仅仅是在小提琴中。
  • 刚刚在此处将小提琴代码添加到 sn-p 中

标签: javascript html arrays json


【解决方案1】:

检查您的控制台是否有错误,大多数时候您会发现打印了相关的错误堆栈。问题出在这里:

var fullColumn = document.getElementById('fullColumn');
var leftColumn = document.getElementById('leftColumn');
var rightColumn = document.getElementById('rightColumn');

这里

<div class="col-lg-6 leftColumn">
  <div class="leftContent" id="leftContent" style=" height: 100%; ">
  </div>
</div>

您正在通过 ID 查询 html 使用类的位置(例如 leftColumn)。使用querySelector/querySelectorAllgetElementsByClassName 或为您的html 元素设置正确的ID。

还有一些未定义的变量,例如leftColumnQtr,您正试图从中访问未定义变量的样式属性。

查看更新的工作 jsfiddle http://jsfiddle.net/gq0t4j93/74/

【讨论】:

  • 哦,你的意思是列?起初我不明白,因为我使用内容 div 的类和 ID,但列 div 没有 ID
  • 对不起,你能检查一下jsfiddle.net/gq0t4j93/61 我解决了这些错误,但在第一次迭代后它似乎仍然没有显示任何内容
  • 第一个似乎工作正常,但之后只是空白,我想知道这是否与我的 if/else 语句有关
  • 您的style.display = "none" 有问题。一旦你隐藏它们,你就永远不会取消隐藏它们。此外,您有奇怪的嵌套条件,其中 page_id == 3 永远无法到达,因为它在 page_id == 2 条件内。确保正确缩进代码以提高可读性,以避免犯此类错误。查看更新的jsfiddle.net/gq0t4j93/74
猜你喜欢
  • 2015-03-16
  • 2014-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多