简体   繁体   中英

Comparing two arrays and getting out missing/wrong items

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?

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..

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)

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']

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 })

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM