[英]Javascript two “For” cycle loop to get data from 3 arrays
I have three arrays one title array with 10 elements, 2nd subtitle array with 121 elements and 3rd description array with 121 elements, i need to display it in a list where i put in this format: 我有三个数组,一个标题数组包含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
---------------------
i have this: 我有这个:
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>");
}
}
but it shows like this: 但它显示如下:
---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
Because it's exactly what are you telling it to do - "translated" it to more human language, it'll look something like: 因为这正是您要执行的操作-将其“翻译”为更多的人类语言,所以它看起来像:
for each title print all the subtitles
You would need something like 您将需要类似
fore each title print all the MATCHING subtitles
that means you would have to change your structure a bit (eg. have array where each element of such array would be object containing title
and another array containing every matching subtitles) 这意味着您将不得不稍微改变结构(例如,具有数组,其中该数组的每个元素都是包含
title
对象,而另一个数组则包含每个匹配的字幕)
Or if you for some reason need to preserve your structure you may do something like this: 或者,如果出于某种原因需要保留您的结构,则可以执行以下操作:
for each title do following:
read all the subtitles and if it's matching print it
that means you will add one if
in your inner for
, that means it'll be looking like this: 这意味着你将添加一个
if
在你的内心for
,这意味着它会看起来像这样:
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 ***/
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.