How can you compare to objects with same properties cause we know both the objects all though same values but sits at different memory location hence they will be not equal.
var user1 = {name : "nerd", org: "dev"};
var user2 = {name : "nerd", org: "dev"};
var eq = user1 == user2;
alert(eq); // gives false
Taken from Andrei course for Advance Javascript
Well a simple solution could be this
var user1 = {name : "nerd", org: "dev"};
var user2 = {name : "nerd", org: "dev"};
var eq = JSON.stringify(user1) == JSON.stringify(user2);
alert(eq);
By converting the objects into sting the values can be compared but we have to be care full of the spaces and cases to be exact in both the objects.
A deep dive discussion for the same can be found on the stackoverflow page. The Page
Top comments (15)
Nice way but small problem with it is if the object aren’t order the same way you gonna get false
Unlike lodash isEqual that check for the key itself
You can check if object are similar by go our there keys and if you dont find something it not equal
Agreed, one should use a utility like lodash or underscore for this. Or write your own recursive function and use Object.keys.
To expand on what I believe Tal means regarding ordering --
An object's keys are ordered in the same order they were inserted (unless the keys look like numbers, in which case they're ascending)
Therefore:
JSON.stringify({a:1,b:2}) == JSON.stringify({b:2,a:1}) // false
I disagree. I wouldn't want to import a library to check such small thing. I can write a small function that recursively iterates and compares values of the properties.
Ya it depend if you use but the main concepts here its to use something more reliable then json strigify
And lodash is pretty small library
ya thanks you very much
Agree, but partially. An object is not a data structure in which an order is defined, right? Not sure if that would be a concern when comparing JSON objects.
" An object is not a data structure in which an order is defined, right?"
Yes and no. An object (any js object) has a list of keys that does have an explicit order. That order is first if the key can parse to a number, then the keys are ordered ascendingly, and then if the key cannot parse to a number, then the keys are sorted in the order they're inserted.
This is pretty specific to ecmascript/javascript, other languages have different rules for dynamic maps. In all languages however when creating equality rules for dynamic map-like structures, order isn't expected to matter for comparison. (Which is why stringify is the wrong choice for object comparison)
Example:
Because those keys do have an order, if you used stringify as your comparison, {a: 1, b: 2} which you would expect to equal {b: 2, a: 1}, does not.
Iterating over keys has the problem that nested objects need to be considered recursively. The JSON-comparison is also fairly expensive, however.
so you can add if your or an object because check with JSON.stringify means if it's not order exactly the same it false
Notice that by using JSON.stringify the order of the fields matter…
Yup it does
Not a reliable method bro! also if the json is too huge, the good bye performance!
Yup you are right this is just for fun question not to be used in production 😜
Stringify is useless without replacer and reviver to compare objects.
As you all can see in the end I have a stackoverflow link so refer that for similar discussion with some solutions as well.