16

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

captainrad
  • 3,760
  • 16
  • 41
  • 74
  • You might want to look up how the merge step occurs in a Merge Sort... – Cameron Mar 09 '12 at 18:31
  • 2
    uh - what is the question, exactly? – Aprillion Mar 09 '12 at 18:32
  • updated it to include a clearer question at the bottom. sorry for the confusion. – captainrad Mar 09 '12 at 18:41
  • `courseHwork[i].duedate` suggests `courseHwork` is an array of objects, why did you declare it as an array of strings in the beginning? – Aprillion Mar 09 '12 at 18:45
  • @deathApril, that was simply an example I was aware my question might be difficult to understand and I had a hard time wording it. however in the js comments and the 'note' at the end I mentioned the correct circumstances. – captainrad Mar 09 '12 at 18:49

4 Answers4

15

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/

Kedor
  • 1,488
  • 5
  • 29
  • 53
  • Glad it helped, tho i was using example arrays, not arrays of object as deathApril noticed you have. But changing that shouldn't be a problem i hope! – Kedor Mar 09 '12 at 18:58
  • Please post relevant code here as well as providing the jsfiddle link. – cambraca Mar 09 '12 at 19:58
4

You can do something like this:

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

var arr = daysArray.concat(courseHwork);
var sorted_arr = arr.sort();
var results = [];
for (var i = 0; i < arr.length - 1; i++) {
    if (sorted_arr[i + 1] == sorted_arr[i]) {
        results.push(sorted_arr[i]);
    }
}

console.log(results);

You can run the code here: http://jsfiddle.net/WtEwJ/2/

This will result in a new array results that only contains the duplicate items.

Paul
  • 12,392
  • 4
  • 48
  • 58
2

Hi using lodash or underscore you can easily intersection two array.

_.intersection([2, 1], [2, 3]);

Result is [2].

Lodash document: https://lodash.com/docs/4.17.2#intersection

Yogesh Patel
  • 314
  • 1
  • 11
2

i think you should first create an array intersection and then iterate over the result to do your thing...

Community
  • 1
  • 1
Aprillion
  • 21,510
  • 5
  • 55
  • 89
  • Underscore.js offers such a method http://documentcloud.github.com/underscore/#intersection and many others. – Tuan Mar 09 '12 at 18:58