Answer the question
In order to leave comments, you need to log in
Comparing two json objects?
I would be very grateful for your help!
I have two json objects and work with them in jquery . The first json->arr1 I get when the button is pressed, it can have a different number of objects 10,100, 200 .... I display them on the map
If the user performs actions, then an ajax request is triggered that receives json->arr2 and in it I get a different number of objects 7,35, 178....
1) I want to get the difference between two json objects, that is, comparing arr1 and arr2 to get the object
{Lattitude: '52,4043000', Location: 'a2', Longitude: '55,7181815', здесь у меня ещё какие-то параметры}
because when comparing it is not in arr2var uniqueAll = $.unique($.merge(arr1, arr2));
arr1 = [
{Lattitude: '52,4043000', Location: 'a2', Longitude: '55,7181815', здесь у меня ещё какие-то параметры},
{Lattitude: '52,3882320', Location: 'b2', Longitude: '55,7225500', здесь у меня ещё какие-то параметры},
{Lattitude: '52,4041184', Location: 'c2', Longitude: '55,7172296', здесь у меня ещё какие-то параметры},
{Lattitude: '52,3996194', Location: 'd2', Longitude: '55,7200249', здесь у меня ещё какие-то параметры},
];
arr2 = [
{Lattitude: '52,3882320', Location: 'b2', Longitude: '55,7225500', здесь у меня ещё какие-то параметры},
{Lattitude: '52,4041184', Location: 'c2', Longitude: '55,7172296', здесь у меня ещё какие-то параметры},
{Lattitude: '52,3996194', Location: 'd2', Longitude: '55,7200249', здесь у меня ещё какие-то параметры}
];
function objDiff(arr1, arr2) {
var resultArray = [];
for (var i = 0; i < arr1.length; i++) {
var found = false;
for (var j = 0; arr2.length; j++) {
//здесь вываливается ошибка arr2[j].Lattitude Undefined, но в json Объектах arr1,arr2 имеется всё нигде null-нет в Lattitude и Longitude
if (arr1[i].Lattitude== arr2[j].Lattitude && arr1[i].Longitude== arr2[j].Longitude) {
found = true;
break;
}
}
if (found == false) {
resultArray.push(arr1[i]);
}
}
return resultArray;
}
Answer the question
In order to leave comments, you need to log in
Option 1
function objDiff(arr1, arr2) {
var resultArray = [];
for (var i = 0; i < arr1.length; i++) {
var found = false;
for (var j = 0; j < arr2.length; j++) {
if (arr1[i].Lattitude== arr2[j].Lattitude && arr1[i].Longitude== arr2[j].Longitude) {
found = true;
break;
}
}
if (found == false) {
resultArray.push(arr1[i]);
arr2.push(arr1[i]);
}
}
return resultArray;
}
function convert(arr) {
for(var k = 0, newArr = []; k < arr.length; k++ ){
newArr.push(arr[k].Lattitude+';'+arr[k].Longitude);
}
return newArr;
}
function diff(arr1, arr2) {
var tmpArr1 = convert(arr1),
tmpArr2 = convert(arr2),
result = [];
for(var i = 0; i < arr1.length; i++){
if(tmpArr2.indexOf(tmpArr1[i]) == -1) {
result.push(arr1[i]);
arr2.push(arr1[i]);
}
}
return result;
}
arr1 = [
{Lattitude: '52,4043000', Location: 'a2', Longitude: '55,7181815'},
{Lattitude: '52,3882320', Location: 'b2', Longitude: '55,7225500'},
{Lattitude: '52,4041184', Location: 'c2', Longitude: '55,7172296'},
{Lattitude: '52,3996194', Location: 'd2', Longitude: '55,7200249'},
{Lattitude: '52,3996194', Location: 'd2', Longitude: '55,7200240'},
];
arr2 = [
{Lattitude: '52,3882320', Location: 'b2', Longitude: '55,7225500'},
{Lattitude: '52,4041184', Location: 'c2', Longitude: '55,7172296'},
{Lattitude: '52,3996194', Location: 'd2', Longitude: '55,7200249'}
];
console.log(diff(arr1, arr2))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question