Javascript二“;对于“;循环从3个数组中获取数据

Javascript two "For" cycle loop to get data from 3 arrays

本文关键字:数组 获取 3个 数据 循环 对于 Javascript      更新时间:2023-09-26

我有三个数组——一个标题数组有10个元素,第二个字幕数组有121个元素,而第三个描述数组有121元素,我需要在列表中显示它,我把它放在下面的格式中:

  ---Title
    ---subtitle[0]----listequivalent[0]
    ----subtitle[1]-----list equivalent[1]
    ---subtitle..etc
    ---------------------
    ----title 2
   ---- subtitleofthis[2]-----descriptionEquivalent[2]   
    ----subtitleofthis[3]-----descriotionEquivalent[3] subtitleofthis...etc
    ---------------------

我有这个:

   for(var j=0;j<titles.length;j++){
    $("#divisionTitles").append("<div  class='content-block-title'>" + titles[j] + "</div>");

   for(var i=0;i<subtitle.length;i++){

    $("#divisionTitles").append(" <ul>"+
            "<li class='item-link item-content'>"+
            " <a href='=" + description[i] +"'"  + ">"+
    "<div class='item-inner'>"+
     "<div class='item-title'>"+ subtitle[i] +"</div>"+
    "</div>"+
    "</a>"+
    "</li>"+
    "</ul>");
  }
}

但它显示为:

---title
---subtitle[all] //all from 1 to 121 with the descriptions 1 to 121
--- title 2
--- subtitle[all] //all from 1 to 121 with the descriptions 1 to 121

因为这正是你告诉它要做的——将它"翻译"成更人性化的语言,所以它看起来像:

for each title print all the subtitles

你需要像这样的东西

fore each title print all the MATCHING subtitles

这意味着你必须稍微改变一下你的结构(例如,有一个数组,这个数组的每个元素都是包含title的对象,另一个数组包含每个匹配的字幕)

或者,如果你出于某种原因需要保留你的结构,你可以这样做:

for each title do following:
    read all the subtitles and if it's matching print it

这意味着你将在你的内部for中添加一个if,这意味着它看起来像这样:

for(var j=0;j<titles.length;j++){
    $("#divisionTitles").append("<div  class='content-block-title'>" + titles[j] + "</div>");

   for(var i=0;i<subtitle.length;i++){

       if(subtitle is matching) {
           /* I don't know how the code should find out which
            * title does this subtitle matching because of this
            * i'm not putting exact code in the if but only a psedocode
            */

         /*** do your stuff here ***/
       }
   }
}