0

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?

5
  • Two different objects are never "equal" unless they are literally the same object. not equal properties, but the same object. Commented Aug 19, 2015 at 21:29
  • 1
    keep in mind, 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; }; Commented Aug 19, 2015 at 21:29
  • I just tested this and it seems to work for me, what version of Angular are you using? You can play with this simple example, if you change a value in one of the objects, it switches from Objects are equal to Objects 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. Commented Aug 20, 2015 at 3:41
  • @NicholasSmith I'm using AngularJS 1.2.24 - I think you must be right. The object must be getting decorated with an extra attribute. Now I just need to figure out why/where... Thanks Commented Aug 20, 2015 at 14:35
  • I wonder if reversing the order of parameters to angular.equals would make a difference? Such as angular.equals(current, this.original)? Commented Aug 28, 2015 at 22:24

1 Answer 1

3

Looks like a typo in your code, did you mean to return the value of comparison?

ChangeTracker.protype.hasChanged = function (current) { 
  var comparison = angular.equals(this.original, current);    
  return !compare;  
}; 

Looks like it should have been:

return !comparison

Sign up to request clarification or add additional context in comments.

2 Comments

I concur with your answer.
My mistake. Unfortunately that isn't the issue though. Just a mistake copying into the post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.