In C++ I can just print address of object and check if addresses are same. How can I do something similar in JavaScript ? I can't compare these two objects as I don't have references to them at the same time so I console.log the which gives me very long human readable object which is hard to compare.
Asked
Active
Viewed 219 times
1
-
This question has already been answered here. Hope this helps: https://stackoverflow.com/questions/13685079/how-to-check-if-two-vars-have-the-same-reference – Adam Sep 06 '22 at 14:43
-
@Adam pls put more effort reading my question cause u missed the part where I don't have reference to these objects at the same time – fdfd Sep 08 '22 at 08:03
-
So a question - when you mean 'identical', does this mean that the order of the key/value pairs has to be the same or not? In other words would {'a':'xxx', 'b':'yyy'} be considered identical to {'b':'yyy','a':'xxx'} i.e. is the order of the key/value pairs to be preserved? – Adam Sep 08 '22 at 16:11
-
Can you stringify the two objects then hash them? If they're identical then the hashes will be the same. sha256 has a very low collision rate and you'd only need to compare two 64 character length strings so that could work. There's a hashing function here https://stackoverflow.com/questions/18338890/are-there-any-sha-256-javascript-implementations-that-are-generally-considered-t/48161723#48161723 – Adam Sep 08 '22 at 16:37
1 Answers
1
A solution would greatly differ depending on the type of object you compare, or on what you want to compare between the two.
Since you cannot have references to them at the same time (you could still try to no?) you are correct at dumping the object's value in console... I'd suggest you take both string values and run a comparison on the strings. Maybe with something like https://text-compare.com/ or your IDE.

Salketer
- 14,263
- 2
- 30
- 58