Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 585
captainrad Avatar asked Mar 09 '12 18:03

captainrad


3 Answers

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/

like image 58
Kedor Avatar answered Oct 17 '22 23:10

Kedor


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.

like image 24
Paul Avatar answered Oct 18 '22 00:10

Paul


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

like image 30
Aprillion Avatar answered Oct 18 '22 01:10

Aprillion