Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - A for-loop that compares two arrays looking for matching values

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"];

so in the arrays above there is only one matching value, being: "4"

here's the next part:

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 />");
        }
        }

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.

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.

*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?*

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is idea i came up with:

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>");
        }
    }
}

You can find it working here: http://jsfiddle.net/4cqCE/2/

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?

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...