我有兩個數組不是唯一的元素,由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;
}
您提供了代碼,但沒有說明問題在於什麼。你不想比較所有的屬性嗎?對於比賽,小數點的長度總是相同的嗎?提供可用的數據也有助於任何人對此進行測試。用示例數據創建一個演示 – charlietfl