【发布时间】:2012-03-27 04:47:19
【问题描述】:
我有两个数组需要相互检查,如果它们达到了每个数组中的两个项目实际上彼此相同的程度,那么在某处附加一些 html。
以下是我一直在尝试的一些代码示例:
var daysArray = ["1", "2", "3", "4", "5"];
var courseHwork = ["4", "8", "15", "16", "23", "42"];
所以在上面的数组中只有一个匹配值,即:“4”
这是下一部分:
for (var i = 0; i < courseHwork.length; i++) {
//in my actual code courseHwork contains several objects, each of which
//has a duedate property, so here I want to see if this part of the duedate
//property is equal to any position in the daysArray array.
if (courseHwork[i].duedate.substring(8,10) === daysArray[i]) {
//here I mean to select an element of this class that contains a string
//that is equal to that of the matching positions in both arrays. then
//once found it should take the title property from one of the objects in the
//courseHwork array and append it there.
$('.fc-day-number:contains("'+daysArray[i]+'")').append("<div class='assignment'>"+courseHwork[i].title+"</div><br />");
}
}
如果事情按计划进行,它将找到一个包含字符串“4”的 div,并从 courseHwork 数组中的匹配对象中附加该属性“title”。
注意:我的实际 daysArray 将数字包含为字符串“1”-“31”,并且对象的 courseHwork 数组是动态填充的,因此它可以包含任意数量的对象,但是没有对象的属性值会超过“31” " 在找到的子字符串中。
*问题: 如何循环遍历两个数组,每次在两个数组之间找到匹配值时,都会找到一个也包含完全相同值的 html 元素,然后附加一些内容?*
【问题讨论】:
-
您可能想查看在合并排序中合并步骤是如何发生的...
-
呃 - 到底是什么问题?
-
对其进行了更新,在底部添加了一个更清晰的问题。很抱歉造成混乱。
-
courseHwork[i].duedate暗示courseHwork是对象数组,为什么一开始就声明为字符串数组? -
@deathApril,这只是一个例子,我知道我的问题可能难以理解,我很难措辞。但是在 js cmets 和末尾的“注释”中,我提到了正确的情况。
标签: javascript jquery arrays if-statement for-loop