简体   繁体   English

比较两个数组并找出丢失/错误的项目

[英]Comparing two arrays and getting out missing/wrong items

I have little problem with Js arrays.我对 Js 数组没什么问题。 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 但不在 controlArray 中的项目

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

Items from controlArray but not in fetchArray来自 controlArray 但不在 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 .您可以使用Set#has创建一个Set以更快地访问和过滤。

For getting the values from controlArray switch the arrays.要从controlArray获取值,请切换数组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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