0

I have little problem with Js arrays. I have two arrays - one is correct all the time (created with correct data) and one is coming from fetching basically. I'm trying to compare these two arrays and I'm able to get their matching items, but not the items that aren't matching:

    var results = [];
    var controlArray = ['T', 'M', 'P', 'N']
    var fetchArray = ['T', 'M', 'PP', 'N ']

    for (var i = 0; i < controlArray.length; i++) {
        for (var j = 0; j < fetchArray.length; j++) {
            if (controlArray[i] === fetchArray[j]) {
                results.push(fetchArray[i]);
            }
        }         
    }

Output should be like:

    results = ['PP', 'N '];

or:

    results = ['P', 'N'];

So it would indicate where the problem is. Both of these woukd work.

This gives me matching part. I have tried to put just !== but in that case it throws out basically everything multiple times and I can't see logic why shouldn't it work like that. Also the white space is important.

Any ideas to painlessly get the not matching values out of these arrays?

Kertix
  • 73
  • 1
  • 6
  • Can you share a snippet of the data inside your array? – Harish Soni Feb 10 '20 at 07:25
  • please add the wanted result. are the arrays sorted or do you need only the missing or common parts? – Nina Scholz Feb 10 '20 at 07:26
  • 1
    You can try: `results = controlArray.filter(e => !fetchArray.includes(e)) ` – uminder Feb 10 '20 at 07:27
  • 1
    Does this answer your question? [Compare 2 arrays and show unmatched elements from array 1](https://stackoverflow.com/questions/40537972/compare-2-arrays-and-show-unmatched-elements-from-array-1) – Pushprajsinh Chudasama Feb 10 '20 at 07:27
  • do you want to match first item with first and so on? – Beingnin Feb 10 '20 at 07:28
  • Edited question. Both arrays are sorted. – Kertix Feb 10 '20 at 07:29
  • the problem is that you are comparing each item of first array with each item of the second one. So if you put == it wil return all the items that are equal, and if you put !== it will return every item of the second array that is not equal to a current item of first array for each item first array. For example for first item of controlArray 'M', 'PP', 'N ' not equals 'T', than for the second item 'M' - 'T', 'PP', 'N ' not equals it etc. So the easiest way to do this using js es6 is using filter function as uminder mentioned – xottabych007 Feb 10 '20 at 07:31
  • Does this answer your question? [Compare two Javascript Arrays and remove Duplicates](https://stackoverflow.com/questions/14930516/compare-two-javascript-arrays-and-remove-duplicates) – Punit khandelwal Feb 10 '20 at 07:46

5 Answers5

1

You should loop over an array and filter using includes. For example:

const results = fetchArray.filter(el => !controlArray.includes(el));
// results: ['PP', 'N ']

Hope it helps..

azatprog
  • 304
  • 2
  • 4
1

The reason why it did returned is you a comparing on element of an array with all the other elements of another array.

var results = [];
var controlArray = ['T', 'M', 'P', 'N']
var fetchArray = ['T', 'M', 'PP', 'N ']

for (var i = 0; i < controlArray.length; i++) {
    for (var j = 0; j < fetchArray.length; j++) {
        if (controlArray[i] === fetchArray[j]) {
            results.push(fetchArray[i]);
        }
    }         
}

instead of this you should take only one index to compare both the arrays.

    var results = [];
    var controlArray = ['T', 'M', 'P', 'N']
    var fetchArray = ['T', 'M', 'PP', 'N ']

    for (var i = 0; i < controlArray.length; i++) {
        if (controlArray[i] !== fetchArray[i]) {
            results.push(fetchArray[i]);
        }       
    }
    console.log(results)
sathish1409
  • 245
  • 3
  • 6
0

Items from fetchArray but not in controlArray

fetchArray.forEach(function(it){
    controlArray.indexOf(it)==-1&&results.push(it)
})// results: ['PP', 'N ']

Items from controlArray but not in fetchArray

controlArray.forEach(function(it){
    fetchArray.indexOf(it)==-1&&results.push(it)
})// results: ['P', 'N']
Beingnin
  • 2,288
  • 1
  • 21
  • 37
0

You can just use filter and find the matching and not matching elements.

const controlArray = ['T', 'M', 'P', 'N'];
const fetchArray = ['T', 'M', 'PP', 'N '];

const match = fetchArray.filter(value => !!controlArray.find(controlValue => value === controlValue))

const noMatch = fetchArray.filter(value => !controlArray.find(controlValue => value === controlValue));

console.log({
  match,
  noMatch
})
thopaw
  • 3,796
  • 2
  • 17
  • 24
0

You could vreate a Set for faster access and filter with Set#has.

For getting the values from controlArray switch the arrays.

var controlArray = ['T', 'M', 'P', 'N'],
    fetchArray = ['T', 'M', 'PP', 'N '],
    controlSet = new Set(controlArray),
    result = fetchArray.filter(v => !controlSet.has(v));

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392