Page 1 of 1

Javascript: Object Equivalence

Posted: Thu Aug 17, 2006 9:56 am
by tomprogers
Is it possible to easily check if two different objects are identical? The following code refuses to return true, so unless there's a special technique I'm unaware of, it's not going to be simple:

Code: Select all

var A = {one: 'bob', two: 'six', th3: [1,2,3]};
var B = {one: 'bob', two: 'six', th3: [1,2,3]};
if(A == B || A === B) return true;
if(eval(A) == eval(B) || eval(A) === eval(B)) return true;
return false;
If I have to loop through properties, so be it, but I'd really prefer something like this, that's automatic. Thanks.

Posted: Thu Aug 17, 2006 12:15 pm
by Weirdan
JS Manual wrote: Two objects are equal if they refer to the same Object

Posted: Thu Aug 17, 2006 12:25 pm
by Weirdan
In Mozilla you can use toSource() method to convert objects to strings and then compare resulting strings

Gah

Posted: Thu Aug 17, 2006 1:02 pm
by tomprogers
I kind of figured that was the case. Looks like I'm going to be doing property comparisons.

Thanks.