比较两个数组以寻找匹配值的 for 循环 [英] A for-loop that compares two arrays looking for matching values

查看:61
本文介绍了比较两个数组以寻找匹配值的 for 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组需要相互检查,如果它们已经达到每个数组中的两个项目实际上彼此相同的程度,则在某处附加一些 html.

I have two arrays that I need to check against each other and if they have reached a point where both items in each array are actually the same as one another, then append some html somewhere.

以下是我一直在尝试的一些代码作为示例:

Here are some bits of the code I have been trying as an example:

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

因此在上面的数组中只有一个匹配值,即:4"

so in the arrays above there is only one matching value, being: "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".

If things worked as planned it will have found a div, that contains the string "4" and appended that property 'title' from the matching object in the courseHwork array.

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

note: my actual daysArray covers numbers as strings "1"-"31", and the courseHwork array of objects is dynamically populated so it can contain any number of objects, however no object will have a property value that exceeds "31" in the substring being found.

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

*QUESTION: How can I loop through two arrays and each time that matching values are found between the two arrays, an html element is found that also contains that exact same value, then has something appended to it?*

推荐答案

这是我的想法:

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 值,如果找到,则将某些内容附加到其中包含4"的 div 中.这就是你想要的吗?

Well, check if that what you want. First it looks for SAME value in 2 arrays, and if it finds it, it appends something to div containing "4" in it. Is that what you want?

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

Here is an example with 2 same values, with 2 divs, each containing one of the value. http://jsfiddle.net/4cqCE/3/

这篇关于比较两个数组以寻找匹配值的 for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆