2015-10-28 96 views
0

我有兩個數組不是唯一的元素,由JSON工程與jQuery陣列,如何比較和刪除陣列

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)如何比較兩個數組和數組2只獲得獨特的元素和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; 
} 
+0

您提供了代碼,但沒有說明問題在於什麼。你不想比較所有的屬性嗎?對於比賽,小數點的長度總是相同的嗎?提供可用的數據也有助於任何人對此進行測試。用示例數據創建一個演示 – charlietfl

回答

0

首先,您發佈的內容是無效的JSON。如果這是你實際代碼的一部分,你需要處理你的JSON輸出。您可以將其插入http://jsonlint.com/進行驗證。如果它不是您的實際代碼,那麼發佈一些實際的JSON代碼會很有幫助。

得到一個陣列的不同元件可以使用jQuery獨特()

var unique = $.unique(array2); 

至2個陣列組合可以使用jQuery合併();

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

擺脫2個陣列的不同值只是將二者結合起來:

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

非常感謝Gregg,我知道我寫的JSON數組是無效的,我只是試圖描述我的示例 –

+0

,並且我有最後一個佇列 –

0

這裏有一個Fiddle和代碼。這種變體是香草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/