0

I am trying to compare two arrays and display the values that do not match.

Using jQuery, I have this:

$.each(array1, function(key, value)
{
  var index = $.inArray(value, array2);
  if(index != -1)
  {
    console.log(index);
  }
});

Using the above code, I can find the array elements that match.

For example, the array1 looks like this:

"GESU687543", "TCNU315504", "TGHU394463"

array2 looks like this:

"TCNU315504", "TRIU805499", "CMAU029901", "GESU687543", "TGHU394463", "NEUL0325B"

But the console will only display 3, 0, 4 because those values exist in both arrays. I want to do the exact opposite. I need to find the values that don't match and display those values to the page.

So, using my examples above, the values that should be displayed to the console are:

"TRIU805499", "CMAU029901", "NEUL0325B"

Edit

SO advised me that this question is possibly a duplicate: How to get the difference between two arrays in Javascript?

With that said, that page is using JavaScript. I'm strictly using jQuery here.

halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89

4 Answers4

2

You need to switch the arrays and iterate array2 for filtering with the index of array1.

var array1 = ["GESU687543", "TCNU315504", "TGHU394463"],
    array2 = ["TCNU315504", "TRIU805499", "CMAU029901", "GESU687543", "TGHU394463", "NEUL0325B"],
    result = array2.filter(function (a) {
        return array1.indexOf(a) === -1;
    });

console.log(result);

ES6 with Array#includes:

var array1 = ["GESU687543", "TCNU315504", "TGHU394463"],
    array2 = ["TCNU315504", "TRIU805499", "CMAU029901", "GESU687543", "TGHU394463", "NEUL0325B"],
    result = array2.filter(a => !array1.includes(a));

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1
var array3;
for(var i=0, l=array1.length; i<l;i++){
    for(var j=0, k=array2.length; j<k; j++){
         if(array1[i] != array[2]){
            aray3.push(array[i]);
         }
    }
}
praveen
  • 489
  • 6
  • 12
  • Please try to avoid just dumping code as an answer and try to explain what it does and why. Your code might not be obvious for people who do not have the relevant coding experience. Please edit your answer to include [clarification, context and try to mention any limitations, assumptions or simplifications in your answer.](https://stackoverflow.com/help/how-to-answer) – Frits Jun 30 '17 at 10:03
  • praveen this is great can you also give answer to get all values that are not similar into array4 .. so array3 is all values that are similar and array4 will be values that have changed ... – user3548161 Aug 10 '17 at 10:20
0

You should iterate array2, not array1.

example:

var array1 = ["GESU687543", "TCNU315504", "TGHU394463"];
var array2 = ["TCNU315504", "TRIU805499", "CMAU029901", "GESU687543", "TGHU394463", "NEUL0325B"];

array2.forEach(value => array1.indexOf(value) === -1 && console.log(value));
giarmul
  • 37
  • 8
-1

So why don't you just change the if(index != -1) to if(index === -1).