Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have these two arrays and I am trying to throw an alert if both arrays have any of the same element in them, any body have any experience with this?

C#
var selectedFacils = [];
var selectedExcludeFacils = [];
$("#selectFacilParams_chosen").find(".search-choice").each(function () {
    var elem = jQuery(this).text();
    selectedFacils.push(elem);
});
$("#selectFacilParamsExclude_chosen").find(".search-choice").each(function () {
    var elem = jQuery(this).text();
    selectedExcludeFacils.push(elem);
});
Posted
Updated 23-Jul-20 0:45am
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 10:28am    
Any on the same position, or compare all with all?
—SA
JasonMacD 4-Mar-14 10:33am    
Positioning doesn't matter, just if there are any matches at all anywhere.

Thank you for the clarification. It means that you need to perform N * M comparisons, where N and M are lengths of the two arrays. Do it in two nested loops.

This is simple is your array keys are consecutive integers and you know the sizes. In Javascript, any array is an associative container with any keys. If this is the concern, you can use Object.keys and iterate by keys. Please see:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys[^].

For better alternative, analyze how you end up having two different arrays with duplicate objects and think at preventing this situation in first place, when you add objects to the array.

—SA
 
Share this answer
 
v2
Comments
Abhinav S 4-Mar-14 11:07am    
5. I recommended a slightly different approach though.
Sergey Alexandrovich Kryukov 4-Mar-14 11:21am    
Thank you, Abhinav.
—SA
If positions do not matter and the array lengths are same, I would recommend you sort the two arrays and then compare the array values one by one.

If the array lengths are unequal, you can work on the principles of the merge sort algorithm, and get a list of items that are common across both arrays.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 11:21am    
Makes sense, could optimize it, a 5.
—SA
Abhinav S 4-Mar-14 11:27am    
Thank you SA.
JasonMacD 4-Mar-14 11:30am    
The array lengths most likely would not be the same, these are param inputs (included/excluded). If an input is included it cannot be excluded and vice versa. Any code examples on this?
Abhinav S 4-Mar-14 11:40am    
https://vikasrao.wordpress.com/tag/compare-2-arrays-javascript/


var a1=[{empName:"Hegde"},{empName:"vicky"}]
var a2=[{empName:"Hegde",salary:30000},{empName:"savrav",salary:2323}]

function f(a1,a2){
var b=[];
for(var i=0;i<a2.length;i++) {
var found = false;
for(var j=0;j<a1.length;j++) {
if(a1[i].empName==a2[j].empName){
found = true;
}
}
if(!found) {
b.push(a2[i].empName);
}
}
return b;
}
console.log(f(a1,a2));


 
Share this answer
 
Comments
CHill60 23-Jul-20 7:00am    
Essentially a rehash of solution 1 from 6 years ago

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900