Javascript: Object Equivalence

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Javascript: Object Equivalence

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

JS Manual wrote: Two objects are equal if they refer to the same Object
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

In Mozilla you can use toSource() method to convert objects to strings and then compare resulting strings
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Gah

Post by tomprogers »

I kind of figured that was the case. Looks like I'm going to be doing property comparisons.

Thanks.
Post Reply