Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a partial, naïve solution to my problem - I will update this as I further develop it.</p> <pre><code>function findDifferences(objectA, objectB) { var propertyChanges = []; var objectGraphPath = ["this"]; (function(a, b) { if(a.constructor == Array) { // BIG assumptions here: That both arrays are same length, that // the members of those arrays are _essentially_ the same, and // that those array members are in the same order... for(var i = 0; i &lt; a.length; i++) { objectGraphPath.push("[" + i.toString() + "]"); arguments.callee(a[i], b[i]); objectGraphPath.pop(); } } else if(a.constructor == Object || (a.constructor != Number &amp;&amp; a.constructor != String &amp;&amp; a.constructor != Date &amp;&amp; a.constructor != RegExp &amp;&amp; a.constructor != Function &amp;&amp; a.constructor != Boolean)) { // we can safely assume that the objects have the // same property lists, else why compare them? for(var property in a) { objectGraphPath.push(("." + property)); if(a[property].constructor != Function) { arguments.callee(a[property], b[property]); } objectGraphPath.pop(); } } else if(a.constructor != Function) { // filter out functions if(a != b) { propertyChanges.push({ "Property": objectGraphPath.join(""), "ObjectA": a, "ObjectB": b }); } } })(objectA, objectB); return propertyChanges; } </code></pre> <p>And here is a sample of how it would be used and the data it would provide (please excuse the long example, but I want to use something relatively non-trivial):</p> <pre><code>var person1 = { FirstName : "John", LastName : "Doh", Age : 30, EMailAddresses : [ "john.doe@gmail.com", "jd@initials.com" ], Children : [ { FirstName : "Sara", LastName : "Doe", Age : 2 }, { FirstName : "Beth", LastName : "Doe", Age : 5 } ] }; var person2 = { FirstName : "John", LastName : "Doe", Age : 33, EMailAddresses : [ "john.doe@gmail.com", "jdoe@hotmail.com" ], Children : [ { FirstName : "Sara", LastName : "Doe", Age : 3 }, { FirstName : "Bethany", LastName : "Doe", Age : 5 } ] }; var differences = findDifferences(person1, person2); </code></pre> <p>At this point, here is what the <code>differences</code> array would look like if you serialized it to JSON:</p> <pre><code>[ { "Property":"this.LastName", "ObjectA":"Doh", "ObjectB":"Doe" }, { "Property":"this.Age", "ObjectA":30, "ObjectB":33 }, { "Property":"this.EMailAddresses[1]", "ObjectA":"jd@initials.com", "ObjectB":"jdoe@hotmail.com" }, { "Property":"this.Children[0].Age", "ObjectA":2, "ObjectB":3 }, { "Property":"this.Children[1].FirstName", "ObjectA":"Beth", "ObjectB":"Bethany" } ] </code></pre> <p>The <code>this</code> in the <code>Property</code> value refers to the root of the object that was compared. So, this solution is not yet <em>exactly</em> what I need, but it is pretty darn close. </p> <p>Hope this is useful to someone out there, and if you have any suggestions for improvement, I am all-ears; I wrote this very late last night (i.e. early this morning) and there may be things I am completely overlooking.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload