[英]Loop through array and match items to html elements
I am looking for a more intuitive way to run the code below (as it is also incomplete to my purpose). 我正在寻找一种更直观的方式来运行下面的代码(因为它也不完整我的目的)。
for (j = 0; j<items.length; j++) {
var indivItem = items[j];
if (indivItem.category == 1) {
$('.indiv_category[idnumber="1"]').append('<ul class="left_side_item"><li>'+indivItem.title+'</li></ul>');
}
else if (indivItem.category == 2) {
$('.indiv_category[idnumber="2"]').append('<ul class="left_side_item"><li>'+indivItem.title+'</li></ul>');
}
}
Essentially I need line 3 to check if (indivItem.category > 0)
then look for the element with a matching idnumber
attribute and append the necessary info. 基本上我需要第3行检查
if (indivItem.category > 0)
然后查找具有匹配的idnumber
属性的元素并附加必要的信息。
I need this continue for the length of available .indiv_category
elements. 我需要继续使用可用的
.indiv_category
元素。
Basically a matchup of the all items in the 'items' array
to all of the elements with a matching 'idnumber' attribute to the item in the array that contains the same id number. 基本上是'items'
array
中所有项目与所有元素的匹配,这些元素具有与包含相同ID号的数组中的项匹配的'idnumber'属性。
Remmove the condition and just use the variable items[j].category
in selector. 重新启动条件,只需在选择器中使用变量
items[j].category
。
for (j = 0; j<items.length; j++) {
$('.indiv_category[idnumber="'+ items[j].category + '"]').append('<ul class="left_side_item"><li>'+indivItem.title+'</li></ul>');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.