D
D
damir872015-10-30 09:46:25
JavaScript
damir87, 2015-10-30 09:46:25

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 arr2

2) After performing the 1st action, I want to return these objects and already add them to the map

3) I would like to store all newly added points in one object, that is, when I click on the button for the first time, I got arr1 - 45 objects, brought them to the map, then an ajax request occurs when the map is moved, I get another 25 objects from the database, let's say arr2 - 70 objects, comparing arr1 and arr2 I get 25 unique objects that are not yet on the map, display and then I want to add 25 to arr1, it will turn out 70 objects, then there will be a cyclic comparison of 70 objects in arr1 and loaded arr2 150 again, compare them, etc. -
I wrote such a thing according to the documentation getting unique for my 3) case - var uniqueAll = $.unique($.merge(arr1, arr2));
Sample data:
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', здесь у меня ещё какие-то параметры}
];


The code that is available for comparing arr1 and arr2 does not work.

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

2 answer(s)
I
Ivan Demidov, 2015-10-30
@damir87

for (var j = 0; j < arr2.length; j++) {

D
damir87, 2015-10-30
@damir87

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 question

Ask a Question

731 491 924 answers to any question