我有两个数组,我需要检查对方,如果他们达到了一个点,每个数组中的两个项目实际上是相同的,然后在某个地方附加一些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”.
注意:我的实际日期数组包含数字作为字符串“1” – “31”,并且对象的课程Hwork数组是动态填充的,因此它可以包含任意数量的对象,然而没有对象将具有超过“31”的属性值发现子字符串.
*题:
我如何循环遍历两个数组,并且每次在两个数组之间找到匹配的值时,发现一个html元素也包含完全相同的值,然后附加一些东西?
解决方法
这是我想出的想法:
var daysArray = ["1","5","12"]; var courseHwork = ["4","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值,如果它找到它,它会附加一些包含“4”的div.那是你要的吗?
这是一个有2个相同值的例子,有2个div,每个div都包含一个值.
http://jsfiddle.net/4cqCE/3/