我有两个数组,我需要相互检查,如果它们已经达到一个点,每个数组中的两个项目实际上彼此相同,则在某处附加一些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 />");
        }
        }

如果一切按计划进行,它将找到一个div,其中包含字符串“4”,并在courseHwork数组中的匹配对象后附加该属性“title”。

注意:我实际的daysArray涵盖数字为字符串“1”-“31”,并且对象的courseHwork数组是动态填充的,因此它可以包含任意数量的对象,但是没有对象的属性值会超过找到子字符串。

* 问题:
我该如何遍历两个数组,并且每次在两个数组之间找到匹配的值时,都会发现一个html元素,其中也包含完全相同的值,然后附加了某些内容?*

最佳答案

这是我想出的主意:

var daysArray = ["1", "2", "3", "4", "5", "12"];
var courseHwork = ["4", "8", "15", "16", "23", "42", "12"];

for (var i = 0; i < courseHwork.length; i++) {
    for (var j = 0; j < daysArray.length; j++) {
        if (courseHwork[i] == daysArray[j]) {
          $('div:contains("'+daysArray[j]+'")').append("<div class='assignment'>"+courseHwork[i]+" - appended</div>");
        }
    }
}

您可以在这里找到它的工作方式:http://jsfiddle.net/4cqCE/2/

好吧,检查一下你想要什么。首先,它在2个数组中查找SAME值,如果找到它,它将在div中附加包含“4”的值。那是你要的吗?

这是一个示例,其中包含2个相同的值,具有2个div,每个div包含一个值。
http://jsfiddle.net/4cqCE/3/

09-16 04:15
查看更多