I have an AJAX request. Runs every five seconds.

$.ajax({ type: "GET", url:"{{URL::action('GpsController@GPSOnline') }}", dataType: 'json', // Выводим координаты из 1.php success: function(data){ myCollection.removeAll() ; console.log(data); var a = data; if (a.length > 0) { for (var i=0;i<a.length;i++) { var ch = a[i].ch, poz=a[i].poz, la = a[i].la, lo =a[i].lo; var img = "/../../../images/poz.php?ch="+ch+"&poz="+poz+"&rnd="+poz; console.log(img); ymaps.option.presetStorage.add("my#customIcon"+ch+poz, { iconImageHref: img, iconImageOffset: [-12, -12], iconImageSize: [34, 34] }); myPlacemark = new ymaps.Placemark( [la, lo],{iconContent:"" }, {preset: "my#customIcon"+ch+poz}, {draggable: true}); myCollection.add(myPlacemark); b = myCollection ; }} myMap.geoObjects.add(myCollection); function getZoom(){ if (document.getElementById('checks').checked==true){ // Автомассштаб myMap.setBounds(myMap.geoObjects.getBounds()); } else { } } getZoom(); } , error: function(){ alert('AJAX заспрос не отработал!'); } }); } 

How can I transfer the values ​​from the myPlacemark object to the b object and compare them (are they the same or not)?

  • Do you want to compare properties of two objects recursively or what? - zb '
  • Yes, I need to compare 2 objects recursively - CBETOBuT

4 answers 4

There are no special operators or simple methods for comparing two objects in Javascript.

But you can use the wonderful library underscore. You need the function _.isEqual

    There is a belief that comparing objects is moveton (in JS exactly where almost everything is an object, I don’t know about other PL, but this is hardly a good form).
    Just because it makes no sense.
    Usually, several fields are compared if these are request objects and this is enough.
    If you really want to, then you can go through the objects for..in and compare keys and values. True, there are no guarantees - properties can be with the DontEnum flag.
    But ordinary objects (not built-in) and quite simple (without assignments of the hidden flag through Object.defineProperty ) can be checked.
    Flag (not DontEnum :)) in your hand!

      I wrote for myself a small function for comparing objects. I use it to correctly respond to changing parameters in the settings file. Sends as a result different parameters from newObject. If there is no property in newObject, but there is a property in oldObject, this will also be displayed.

       /** * Compare objects and return properties that are differ * @param {Object} newObj Properties from this object will be returned * @param {Object} oldObj Object to compare with * @return {Object} Object with differ properties */ function compareObjects(newObj, oldObj) { 'use strict'; var clone = "function" === typeof newObj.pop ? [] : {}, changes = 0, prop = null, result = null, check = function(o1, o2) { for(prop in o1) { if(!o1.hasOwnProperty(prop)) continue; if(o1[prop] instanceof Date){ if(!(o2[prop] instanceof Date && o1[prop].getTime() == o2[prop].getTime())){ clone[prop] = newObj[prop]; changes++; } }else if (o1[prop] && o2[prop] && "object" === typeof o1[prop]) { if(result = compareObjects(newObj[prop], oldObj[prop])){ clone[prop] = "function" === typeof o1[prop].pop ? newObj[prop] : result; changes++; } }else if(o1[prop] !== o2[prop]){ clone[prop] = newObj[prop]; changes++; } } }; check(newObj, oldObj); check(oldObj, newObj); return changes ? clone : false; } 

        It is better to have the server send only changes, not complete objects.

        And for comparisons, a recursive function that compares to equality, if non-primitive elements are not equal (by reference), then it calls itself recursively. If the primitive objects are not equal, or the property is not in the object, then that's it. Do not forget to go through the properties of the second object and check their presence in the first.

        If there is confidence that, with the same values, the server will return the same json, you can save the server's response as a string somewhere and compare them.