0

I have 2 objects and i want to compare old with new and return the new one if values of object differs. I tryed multiple solutions and it seems i can't achieve this. here is what i've tryed

  var new_pkg ={scooter_id:"TM0060",lat:"45.747646",lng:"21.231496",alt:"99.200"};
    var old_pkg={scooter_id:"TM0060",lat:"25.747746",lng:"31.221496",alt:"100.200"};
    function difference(new_pkg, old_pkg) {
        function changes(new_pkg, old_pkg) {
            return _.transform(new_pkg, function(result, value, key) {


        if (!_.isEqual(value, old_pkg[key])) {
                result[key] = (_.isObject(value) && _.isObject(old_pkg[key])) ? changes(value, old_pkg[key]) : value;
            }
        });
    }
    return changes(new_pkg, old_pkg);
}

i want to return {lat:"45.747646",lng:"21.231496",alt:"99.200"};

  • might be duplicate of https://stackoverflow.com/questions/8572826/generic-deep-diff-between-two-objects – Rahul Patil Dec 09 '19 at 10:38
  • If you want to compare two objects by using built in library so you can reach out to use`npm install deep-diff`. https://www.npmjs.com/package/deep-diff – Imran Shaikh Mar 09 '20 at 08:42

3 Answers3

1

You can compare object values by key and return difference object if there is difference:

var new_pkg ={scooter_id:"TM0060",lat:"45.747646",lng:"21.231496",alt:"99.200"};
var old_pkg={scooter_id:"TM0060",lat:"25.747746",lng:"31.221496",alt:"100.200"};

const getNew = (newObj, oldObj) => {
    if (Object.keys(oldObj).length == 0 
        && Object.keys(newObj).length > 0)
        return newObj;

    let diff = {};
    for (const key in oldObj) {
        if (newObj[key] && oldObj[key] != newObj[key] ) {
            diff[key] = newObj[key]; 
        }
    }

    if (Object.keys(diff).length > 0) 
        return diff;
    
    return oldObj;
}

console.log(getNew(new_pkg, old_pkg));
StepUp
  • 36,391
  • 15
  • 88
  • 148
0

Are they always objects with same properties? Like a model?
If so, then you can loop through one of them and compare values at given key.

  var new_pkg ={scooter_id:"TM0060",lat:"45.747646",lng:"21.231496",alt:"99.200"};
    var old_pkg={scooter_id:"TM0060",lat:"25.747746",lng:"31.221496",alt:"100.200"};

function getANewOneIfPropChanged(oldOne, newOne) {
  for(prop in oldOne) {
    if(oldOne[prop] !== newOne[prop]) {
      return newOne;
    }
  }
  return oldOne;
}

console.log(getANewOneIfPropChanged(old_pkg, new_pkg));

Actually though, according to your rules of taking a new object you can just always use a new one. You take if a prop in new one is different than an old one, but if they are the same then it does not matter if you take a new one or an old one. (Unless references to the object itself are important)

matvs
  • 1,763
  • 16
  • 26
0

Try this :

var new_pkg ={scooter_id:"TM0060",lat:"45.747646",lng:"21.231496",alt:"99.200"};
var old_pkg ={scooter_id:"TM0060",lat:"25.747746",lng:"31.221496",alt:"100.200"};


function compareObj(obj1, obj2) {
    // Create arrays of property names
    var obj1Props = Object.getOwnPropertyNames(obj1);
    var obj2Props = Object.getOwnPropertyNames(obj2);

    // If number of properties is different,
    // objects are not equivalent
    if (obj1Props.length != obj2Props.length) {
        return false;
    }
    
    for (var i of obj1Props) {
     if (obj1[i] !== obj2[i]) {
       return false;
      }
    }

    // If we made it this far, objects
    // are considered equivalent
    return true;
}

if (compareObj(old_pkg, new_pkg) === false) {
 console.log(new_pkg);
}
Debug Diva
  • 26,058
  • 13
  • 70
  • 123