【问题标题】:How to read an array inside another array in an " Json object" -> Jade/PUG.js如何在“Json 对象”中读取另一个数组中的数组 -> Jade/PUG.js
【发布时间】:2022-01-15 21:23:21
【问题描述】:

我正在尝试在 Pug 中使用“Json”“创建”一个模板,但有一点我需要放置大量信息。但对我来说,问题变成了我需要深入阅读一个元素的地方。

-
  var SuperSlidersInfo = {
      'numero': '4',
      'sectionName': 'Dignidad',
      'video': true,
      'youtube': [
        'idVideo',
        'idVideo2',
        ],
      'notes':[
        ['prueba1', 'prueba2'],
        ['segundo1', 'segundo2', 'segundo3'],
      ],
      'img': [
        'NombreImagen',
        'NombreImagen2',
        ],
      'extension': 'jpg',
      'titulo': [
        'Titulo',
        ],
      'parrafo': [
        'Texto',
      ]}
+SuperSlidersPug(SuperSlidersInfo)

mixin SuperSlidersPug(SuperSlidersInfo)
 .container-pc
    .container-sliderAutomatico.no-phone
      div(id=`sliderAutomatico${SuperSlidersInfo.numero}` class= SuperSlidersInfo.video ? "dosVideos-container" : "otraCosa").sliderAutomatico
        each imgName, index in SuperSlidersInfo.img
          //- div(class= SuperSlidersInfo.video ? "dosVideos").sliderAutomatico__section
          div(class= SuperSlidersInfo.video ? "dosVideos" : "otraCosa").sliderAutomatico__section
            .swiper-content
              if SuperSlidersInfo.video
                .section_videoContainer
                  a(href=`#video${SuperSlidersInfo.sectionName}${index}`).section_videoThumbnail--Container
                    img(src=`${imgURL}${SuperSlidersInfo.sectionName}${index + 1}.${SuperSlidersInfo.extension}`, alt="")
                  h3.swiper-content_titulo= `${SuperSlidersInfo.titulo[index]}`
                  .section_video--notes
                    //- -----------------
                    //- Problem
                    //- -----------------
                    //- the "a" below works, but I need to "create" and each, or for, or any loop for to put all the elements in that especific array. 
                    //- a(href=`${SuperSlidersInfo.notes[index]}`).section_video--note
                    
                    //- The "each" below it suppose to works. But when I put the "[index]" it brokes..
                    each note, noteIndex in SuperSlidersInfo.notes[index]
                      a(href=`${note}`).section_video--note

对于一般元素,它运作良好。但是当我需要阅读“笔记”时。这对我来说是不可能的。

但是当我输入each note, noteIndex in SuperSlidersInfo.notes[index] 时,一切都坏了......

所以我需要读取“note”在其数组中的“元素”...

//- Works well
each note, noteIndex in SuperSlidersInfo.notes
//- Or...
each note, noteIndex in SuperSlidersInfo.notes[0]

//- But if I put this It's Broke (And I thought that could works well)
each note, noteIndex in SuperSlidersInfo.notes[index]

index 来自几乎存在于代码顶部的each

我怎么说,它是一个“模板”,所以有时我不知道可能存在多少个“项目”。所以我需要为所有可能存在的元素使用each...

【问题讨论】:

    标签: javascript arrays json oop pug


    【解决方案1】:

    其实很简单。你指的是一个二维数组 (即)Arr[i][j]

    所以要访问notes数组中更深的元素写入

    SuperSlidersInfo.no​​tes[i][j];

    例如, 访问 segundo1 写入

    SuperSlidersInfo.no​​tes[1][0];

    编辑: 您可以使用嵌套的 for 循环,在这种情况下,您无需事先知道项目的数量, 代码:

    let obj = {
        'numero': '4',
        'sectionName': 'Dignidad',
        'video': true,
        'youtube': [
          'idVideo',
          'idVideo2',
          ],
        'notes':[
          ['prueba1', 'prueba2'],
          ['segundo1', 'segundo2', 'segundo3'],
        ],
        'img': [
          'NombreImagen',
          'NombreImagen2',
          ],
        'extension': 'jpg',
        'titulo': [
          'Titulo',
          ],
        'parrafo': [
          'Texto',
        ]}
    
        let notes = obj.notes;
    for(let i =0;i<notes.length;i++){
          let inner = notes[i];
      for(let j=0;j<inner.length;j++){
        console.log(inner[j]);
      }
    }

    【讨论】:

    • 好的,但是我必须“手动”执行此操作。这个想法是“自动”的。因此,为此我建议在第一个 []...[index]... 中添加一个index...但是 Pug 不允许我这样做...因为,在另一个项目中可能存在更多“注释”。或者可能更少......所以我不知道。这个想法是“自动”创建的。
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2021-09-19
    • 2018-01-24
    • 1970-01-01
    • 2021-10-06
    • 2014-08-08
    • 2018-05-28
    • 2023-03-31
    相关资源
    最近更新 更多