适用于jquery数组,如何比较和删除数组中不唯一的元素

时间:2015-10-28 14:03:25

标签: jquery arrays compare

我有两个由JSON组成的数组

array1 = [
            object[0] { Lattitude: 55.7181815
                      Location: 0
                      Longitude: 52.4043} 

            object[1] { Lattitude: 54.7181815
                      Location: 0
                      Longitude: 51.4043 }

            object[n] ......
          ]


array2 = [
            object[0] { Lattitude: 55.7181815
                      Location: 0
                      Longitude: 52.4043}

             object[1] { Lattitude: 54.7181815
                      Location: 0
                      Longitude: 51.4043 }

            object[2] { Lattitude: 54.7277775
                      Location: 0
                      Longitude: 51.7743 }
            object[n] ......
          ]

1)如何比较两个数组并从array2中获取唯一元素并返回resultArray? - 我想得到像这样的结果

resultArray =  [ object[0] { Lattitude: 54.7277775
                      Location: 0
                      Longitude: 51.7743 }
                  object[n] ......
                ]

2)如何比较两个数组并获取array1和array2中的所有唯一元素? - 我想得到像这样的结果

resultArray = object[0] { Lattitude: 55.7181815
                      Location: 0
                      Longitude: 52.4043}
            object[1] { Lattitude: 54.7181815
                      Location: 0
                      Longitude: 51.4043 }

            object[2] { Lattitude: 54.7277775
                      Location: 0
                      Longitude: 51.7743 }
            object[n] ......
          ]


function objDiff(array1, array2) {
    var resultArray = [];

    array2.forEach(function (destObj) {
        var check = array1.some(function (origObj) {
            if (origObj.Lattitude == destObj.Lattitude) array1.splice($.inArray(destObj, array1), 1);
        });
        if (!check) {
            destObj.desc = 'missing in source';
            resultArray.push(destObj);
        }
    });

    array1.forEach(function (origObj) {
        var check = array2.some(function (destObj) {
            if (origObj.Lattitude == destObj.Lattitude) array2.splice($.inArray(origObj, array2), 1);
        });
        if (!check) {
            origObj.desc = 'missing in destination';
            resultArray.push(origObj);
        }
    });

    return resultArray;
}

2 个答案:

答案 0 :(得分:0)

首先,您发布的内容不是有效的JSON。如果这是您实际代码的一部分,则需要处理JSON输出。您可以将其插入http://jsonlint.com/进行验证。如果它不是您的实际代码,那么发布一些实际的JSON代码将会有所帮助。

获取数组的不同元素,您可以使用jQuery unique()

var unique = $.unique(array2);

组合2个数组,你可以使用jQuery merge();

var all = $.merge(array1, array2);

从两个数组中获取不同的值只需将两者结合起来:

var uniqueAll = $.unique($.merge(array1, array2));

答案 1 :(得分:0)

这是一个Fiddle和代码。这个变体是vanilla JS它不包含外部库,即:jQuery:

arr1 = [
    {Lattitude: 'a1', Location: 'a2', Longitude: 'a3'},
    {Lattitude: 'b1', Location: 'b2', Longitude: 'b3'},
    {Lattitude: 'c1', Location: 'c2', Longitude: 'c3'}
];

arr2 = [
    {Lattitude: 'b1', Location: 'b2', Longitude: 'b3'},
    {Lattitude: 'c1', Location: 'c2', Longitude: 'c3'},
    {Lattitude: 'e1', Location: 'e2', Longitude: 'e3'}
];

var uniq1 = uniqueDiff1(arr1, arr2)
var uniq2 = uniqueDiff2(arr1, arr2);

console.log(uniq1);
console.log(uniq2);

/**
 * Returns with an array of all unique coordinate objects from
 * 'array2'
 */
function uniqueDiff1(array1, array2){
    return array1.filter(function (el) {
        return indexOfObject(array2, el) < 0;
    });
}

/**
 * Returns with an array of all unique coordinate objects from
 * 'array1' and 'array2'
 */
function uniqueDiff2(array1, array2){
    var unique1 = array1.filter(function (el) {
        return indexOfObject(array2, el) < 0;
    });
    var unique2 = array2.filter(function (el) {
        return indexOfObject(array1, el) < 0;
    });
    return unique1.concat(unique2);
}

/**
 * Returns with the position of the 'obj' element in
 * 'array' array. If 'array' not contains 'obj'
 * it return -1.
 */
function indexOfObject(array, obj){
    var i = 0,
        pos = -1;
    while(pos === -1 && i<array.length){
        if(compareCoordinates(array[i], obj)){
            pos = i;
        }
        i++;
    }
    return pos;
}

/**
 * Are the two coordinates equal?
 * Two coordinates are only equal if all of their
 * attributes are equal.
 */
function compareCoordinates(obj1, obj2){
    if(obj1.Lattitude === obj2.Lattitude
        && obj1.Location === obj2.Location
        && obj1.Longitude === obj2.Longitude){
            return true;
        } else {
            return false;
        }
}

基本上,你必须实现一个比较对象的函数,并且由于对象的性质,你需要一个可以被对象使用的indexOf方法的实现。

函数uniqueDiff1是您问题#1的答案,函数uniqueDiff2是您回答问题#2。

https://jsfiddle.net/ouajw3k0/