Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Solution 1</strong></p> <p>Use JSON.stringify(obj) to get a string representation of the objects you want to compare. Save the string to a file. Use any diff viewer to compare the text files.</p> <p>Note: JSON.stringify will ignore properties that point to function definitions.</p> <p><strong>Solution 2</strong></p> <p>This could do what you want with some modification, it is a modified version of the function _.isEqual (<a href="http://documentcloud.github.com/underscore/insp" rel="nofollow">http://documentcloud.github.com/underscore/</a>). Please feel free to suggest any modifications! I wrote it to figure out where the first difference between two objects occur.</p> <pre><code>// Given two objects find the first key or value not matching, algorithm is a // inspired by of _.isEqual. function diffObjects(a, b) { console.info("---&gt; diffObjects", {"a": a, "b": b}); // Check object identity. if (a === b) return true; // Different types? var atype = typeof(a), btype = typeof(b); if (atype != btype) { console.info("Type mismatch:", {"a": a, "b": b}); return false; }; // Basic equality test (watch out for coercions). if (a == b) return true; // One is falsy and the other truthy. if ((!a &amp;&amp; b) || (a &amp;&amp; !b)) { console.info("One is falsy and the other truthy:", {"a": a, "b": b}); return false; } // Unwrap any wrapped objects. if (a._chain) a = a._wrapped; if (b._chain) b = b._wrapped; // One of them implements an isEqual()? if (a.isEqual) return a.isEqual(b); // Check dates' integer values. if (_.isDate(a) &amp;&amp; _.isDate(b)) return a.getTime() === b.getTime(); // Both are NaN? if (_.isNaN(a) &amp;&amp; _.isNaN(b)) { console.info("Both are NaN?:", {"a": a, "b": b}); return false; } // Compare regular expressions. if (_.isRegExp(a) &amp;&amp; _.isRegExp(b)) return a.source === b.source &amp;&amp; a.global === b.global &amp;&amp; a.ignoreCase === b.ignoreCase &amp;&amp; a.multiline === b.multiline; // If a is not an object by this point, we can't handle it. if (atype !== 'object') { console.info("a is not an object:", {"a": a}); return false; } // Check for different array lengths before comparing contents. if (a.length &amp;&amp; (a.length !== b.length)) { console.info("Arrays are of different length:", {"a": a, "b": b}); return false; } // Nothing else worked, deep compare the contents. var aKeys = _.keys(a), bKeys = _.keys(b); // Different object sizes? if (aKeys.length != bKeys.length) { console.info("Different object sizes:", {"a": a, "b": b}); return false; } // Recursive comparison of contents. for (var key in a) if (!(key in b) || !diffObjects(a[key], b[key])) return false; return true; }; </code></pre>
    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. 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