In an Angular application I am comparing two objects (The original object to the current object after the user has made changes). The problem is, the two objects are not equal - even before any changes are made. When debugging in the browser the two objects look exactly the same. Below I tried angular.equals() but comparison evaluates to false:
ChangeTracker.protype.hasChanged = function (current) {
var comparison = angular.equals(this.original, current);
return !comparison;
};
I also have tried Underscore's _.isEqual as well as the regular (==) and strict (===) equality operators.
I thought I might be comparing by reference but even when I wrap the objects in JSON.parse(JSON.stringify()) the comparison still returns false.
Why else would two seemingly equal objects not be equal?
console.log()runs in an asynchronous manner, so if the reference point that a variable is assigned to changes before console.log finishes, it will output the most current version. at each of the places you did the console logging, can you make a hard clone and output the clone? here's an easy function for it:var hard_clone = function(obj) { if (null === obj || "object" !== typeof obj) return obj; var copy = obj.constructor(); for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr]; } return copy; };Objects are equaltoObjects are not equal. See: plnkr.co/edit/3ojL8T7d3JeBXVObXtrK?p=preview Sounds like one object is getting decorated with some extra attribute causing them not to be equal.angular.equals(current, this.original)?