0

There seems to be quite a few posts about this around google and on stack overflow, however most of them aren't very descriptive and I'm just having a really hard time understanding. Solutions offered all talk about using an external library, which I cannot use.

I have a JavaScript object that looks similar (not exactly) to this:

var test = {[{ "question": "What are you doing?", "id": 1, "answers": [
                { "answer": "Eating some pizza", "correct": true },
                { "answer": "Working on my PC", "correct": false },
                { "answer": "Sitting in a chair", "correct": true }
            ]}]}

Obviously that is an array, because usually there will be multiple questions. Now what I'm doing is trying to make a page where I can edit the test, but I want to make sure that changes have been made to the test before sending it off to the backend for processing. There isn't any reason to send a request to the server if the test hasn't been modified. This means that if any character has been changed, a space has been added, an answer was changed from correct to incorrect, a question was added or removed, then this method would return true. Else it would return false.

The only third party library I'm allowed to use in AngularJS. I've tried the angular.equals(x,y) method and it didn't yield proper results.

One method that I've thought of using, which should have ideally of worked was the following:

function isEqual(obj1, obj2) {
    return (JSON.stringify(obj1) == JSON.stringify(obj2));
} 

However when a value (Such as a space " ") is added, it will still return true, even though the objects are different. While printing the value, it seems that if the space is at the beginning end of a string it will be trimmed. (The same result as angular.equals) However if a space is added between text, then the check returns false (as expected)

So what is the best way to check an object for equality with pinpoint accuracy?

Would appreciate if all answers are in raw javascript, and not using external libraries.

Hobbyist
  • 15,888
  • 9
  • 46
  • 98
  • Apart from spaces being trimmed by JSON this should not be an issue as `'' !== ' '`. – somethinghere Oct 05 '15 at 14:35
  • @Liam - I have read said question before posting, the "correct" answer is that it's not possible in some scenarios, which mine does not fall under. Other answers are recommending `underscore.js`. One answer also recommends `angular.equals` which I talk about in my question. – Hobbyist Oct 05 '15 at 14:39
  • *"which should have ideally of worked was the following"* No, that won't work reliably, because there is no defined order to the property names of an object in JSON. There didn't used to be in JavaScript either, but now there is as of ECMAScript 2015; the ES2015 definition won't help you, because it depends on the order the properties were created. So unless you want `{a:1,b:2}` and `{b:2,a:1}` to be different... – T.J. Crowder Oct 05 '15 at 14:40
  • @T.J - Alright, so this question was closed (while the solutions on the other question are not what I'm looking for / don't work as I intend). So how am I supposed to get an answer for checking the equality of two objects. Even `angular.equals` trims text when checking for equality between two objects. (Which was also talked about in an answer of the "Already answered" question) – Hobbyist Oct 05 '15 at 14:43
  • I don't see anyone claiming that `angular.equals` does trimming, I'm not sure where you're getting that from. A [trivial experiment](http://jsbin.com/kayijotofe) shows that it doesn't. But I see several answers there not using angular which clearly demonstrate how to determine object equivalence. – T.J. Crowder Oct 05 '15 at 14:50
  • @T.J.Crowder - I'm claiming that after using it. It returns the exact same results as the `JSON.stringify == ` method I provided in every scenario (that I've tested) – Hobbyist Oct 05 '15 at 14:58
  • If you're claiming that `angular.equals` is broken (which trimming would be), you're going to have to prove it. Extraordinary claims and all that. Much, much, much more likely that your test was incorrect. "select isn't broken," after all (and again, note the experiment above). – T.J. Crowder Oct 05 '15 at 14:59
  • @Hobbyist `Angular.equals()` appears to be working fine...http://jsbin.com/fiqivofagu/edit?html,output try it with or without spaces in the values it will update `true` and `false` accordingly. – brso05 Oct 05 '15 at 15:13

0 Answers0