On Feb 23, 6:46 pm, "yagi" <[EMAIL PROTECTED]> wrote:
> Good Day to all,
>
> Does anyone know how to compare two json objects?? i tried the "==="
> operator but it still returns false..
You could try just comparing their toString value, but that is likely
very flakey. Christophe's code seems very obfuscated to me (though I
don't doubt its effectiveness), try plain javascript - you might find
it much simpler to understand.
Below is a simple recursive funciton, it effectively tests ===, so
null != undefined and 0 != '0'.
function testEquivalent(a, b)
{
var result = true;
function typeTest(a, b) {return (typeof a == typeof b)}
function test(a, b) {
if (!typeTest(a, b)) return false;
if (typeof a == 'function' || typeof a == 'object') {
for (var p in a) {
result = test(a[p], b[p]);
if (!result) return false;
}
return result;
}
return (a == b);
}
return test(a, b);
}
// Some tests...
var objA = { a:0, b:5, c:[1,2,3], d:'x'}
var objB = { a:0, b:5, c:[1,2,3], d:'x'}
var objC = { a:0, b:5, c:[1,2,null], d:'0'}
var objD = { a:0, b:5, c:[1,2,undefined], d:0}
alert( testEquivalent(objA, objB) ); // true
alert( testEquivalent(objA, objC) ); // false
alert( testEquivalent(objA, objD) ); // false
alert( testEquivalent(objC, objD) ); // false
--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---