florinstefan8830
florinstefan8830

Reputation: 55

How to compare 2 objects in JavaScript and return difference

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"};

Upvotes: 1

Views: 5513

Answers (3)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27242

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);
}

Upvotes: 0

StepUp
StepUp

Reputation: 38209

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));

Upvotes: 3

matvs
matvs

Reputation: 1873

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)

Upvotes: 0

Related Questions