Just parsing JSON and comparing two objects is not enough, because these will not be exact references to objects (but they can be the same values).
You need to do deep equality.
From http://threebit.net/mail-archive/rails-spinoffs/msg06156.html - this is similar to using jQuery.
Object.extend(Object, { deepEquals: function(o1, o2) { var k1 = Object.keys(o1).sort(); var k2 = Object.keys(o2).sort(); if (k1.length != k2.length) return false; return k1.zip(k2, function(keyPair) { if(typeof o1[keyPair[0]] == typeof o2[keyPair[1]] == "object"){ return deepEquals(o1[keyPair[0]], o2[keyPair[1]]) } else { return o1[keyPair[0]] == o2[keyPair[1]]; } }).all(); } }); Usage: var anObj = JSON.parse(jsonString1); var anotherObj= JSON.parse(jsonString2); if (Object.deepEquals(anObj, anotherObj)) ...
jd. Dec 16 '10 at 21:02 2010-12-16 21:02
source share