比较两个数组并获取非重复(非唯一)值 [英] Comparing two arrays and getting the non duplicate(not unique) values

查看:85
本文介绍了比较两个数组并获取非重复(非唯一)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组数组。我需要得到不在两个数组中的字母。首先,它应检查索引0'a'是否在两个阵列上。如果两者都应该从两者中删除'a'(只是第一个数组中的第一个'a'而不是结尾的那个(索引3)。并使用相同的逻辑去第二个项'b'。

I have two sets of arrays. I need to get the letters that are not in both arrays. First it should check if index 0 'a' is on both arrays. If it is in both than it should delete 'a' from both(just the first 'a' in first array not the one at the end(index 3). And go the the second item 'b' using same logic.

var arrayA = ['a','b','c','a'];

var arrayA = ['a','b','c','a'];

var arrayB = ['a','d','f','c'];

var arrayB = ['a','d','f','c'];

var arrayC = []; // Shoud有结果[b, a,d,f]

var arrayC = []; //Shoud have the result[b,a,d,f]

代码设置在 http://jsfiddle.net/rexonms/AbFYh/#base

HTML

 <p class="arrayA">Array A</p>
 <p class="arrayB">Array B</p>
 <p class="arrayC">Array C</p>


jQuery

​ jQuery

var arrayA = ['a','b','c','a'];
var arrayB = ['a','d','f','c'];
var arrayC = []; //Shoud have the result[b,a,d,f]

$('.arrayA').text('ArrayA: ' + arrayA);
$('.arrayB').text('ArrayB: ' + arrayB);

$.each(arrayA, function(indexA,valueA) {
$.each(arrayB, function(indexB, valueB){

    if(valueA != valueB)
    {
        arrayC.splice(valueA);            
    }
});

$('.arrayC').text('ArrayC: ' + arrayC);
});


推荐答案

这是你想要的工作版本:
http://jsfiddle.net/Zzt5f/

Here is a working version of what you want: http://jsfiddle.net/Zzt5f/

var arrayA = ['a','b','c','a'];
var arrayB = ['a','d','f','c'];
var arrayC = []; //Should have the result[b,a,d,f]

$('.arrayA').text('ArrayA: ' + arrayA);
$('.arrayB').text('ArrayB: ' + arrayB);

$.each(arrayA, function(indexA,valueA) {
    $.each(arrayB, function(indexB, valueB){
        if(valueA == valueB)
        {
            arrayA[indexA]=null;
            arrayB[indexB]=null;
            return false; //break out of inner each loop                     
        }
    });
});

$.each(arrayA.concat(arrayB),function(idx,val) {
    if(val!=null) arrayC.push(val);
});

$('.arrayC').text('ArrayC: ' + arrayC);
alert(arrayC);

如您所见,我只对原始代码进行了一些修改。首先,由于您尝试删除重复值,因此需要检查 valueA == valueB ,反之亦然。找到匹配后,第二次迭代需要暂停以防止删除第二个数组中的第二个副本,因此返回false

As you see I made only a few modifications to your original code. Firstly, since you are trying to remove the duplicate values, you need to check if valueA==valueB, not vice-versa. Once a match has been found, the second iteration needs to halt to prevent removal of a second duplicate in the second array, hence the return false.

我没有使用array.splice方法,因为这实际上是通过从调用它的数组中删除值来创建并返回一个新数组,所以基本上它没有像你那样做任何事情正在使用它。不继续在循环中创建新数组感觉更清晰。请注意,此方法实际上会修改 arrayA arrayB ,因此如果以后再次需要它们,则必须克隆他们。

I didn't use the array.splice method, because this actually creates and returns a new array by removing values from the array it is called on, so essentially it wasn't doing anything the way you were using it. It felt cleaner to not keep creating new arrays within the loop. Note that this method will actually modify arrayA and arrayB, so if you need them again later on you will have to clone them.

这篇关于比较两个数组并获取非重复(非唯一)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆