Object comparison in JavaScript
Posted
by spankmaster79
on Stack Overflow
See other posts from Stack Overflow
or by spankmaster79
Published on 2009-07-01T12:18:29Z
Indexed on
2010/03/26
6:13 UTC
Read the original article
Hit count: 308
JavaScript
What is the best way to compare Objects in JavaScript?
Example:
var user1 = {name : "nerd", org: "dev"};
var user2 = {name : "nerd", org: "dev"};
var eq = user1 == user2;
alert(eq); // gives false
I know that "Two objects are equal if they refer to the exact same Object", but is there a way to check it another way??
Using this way works for me.....but is it the only possibility?
var eq = Object.toJSON(user1) == Object.toJSON(user2);
alert(eq); // gives true
© Stack Overflow or respective owner