1

Is there a way to compare two un-referenced objects by their literal value like a string or a number?

_is = function (a, b) { /* Code */ }

This could apply to any object type, even custom objects.

_is(new X("a"), new X("a")); // Returns True;

_is(new X("a"), new Y("a")); // Returns False

You could convert it into a string, but that would be sloppy.

JSON.stringify({ x: "a" }) == JSON.stringify({ x: "a" }); // Returns True

Maybe there's a way to programatically read each key, subkey, and value of the object, and compare that way.

Any ideas?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
imaxwill
  • 113
  • 8

1 Answers1

2

Javascript hashes objects in memory with unique memory pointers. You're essentially creating two separate objects, and trying to compare them. Despite the fact that they look similar, they in fact are different.

Try storing the object as a variable first, and then testing against that variable.

pass = arg => { return arg }; pass({ a: "x" }); // Returns The Input

pass(({ a: "x" })) == ({ a: "x" }); // Returns False

var obj = {a: "x"};

pass(obj) === (obj); // Returns true

console.log(pass(obj) === (obj));

UPDATE:

If you want to compare two separate objects, the best and most efficient way I've found is to create a hash of both objects, and then compare the hashes (basically comparing two checksums). If the hashes match, the objects are the same. If the hashes differ, they are different objects. You can assume that the properties and values are the same in each object, since using a hashing function, even hashing functions used in some forms of crypto like md5 and sha can be utilized to generate a unique hash of the object.

Here's a 3rd Party Library I've used before to generate object hashes.

Danny Bullis
  • 3,043
  • 2
  • 29
  • 35
  • Alright, it worked. – imaxwill Apr 25 '17 at 21:23
  • Still, It would be cool if there was a way to allow this. – imaxwill Apr 25 '17 at 21:23
  • Yeah, it's by creating a variable and using that, since it ensures you are using the same object reference. – Danny Bullis Apr 25 '17 at 21:24
  • Please mark correct if it's correct :) – Danny Bullis Apr 25 '17 at 21:25
  • it's "correct" but not what I was looking for. Thanks though – imaxwill Apr 25 '17 at 21:33
  • Please mark it correct if it's correct so that others who may stumble upon this question will know the solution is resolved. You cannot achieve what you're asking for any other way. Not sure what else you're "looking for." – Danny Bullis Apr 25 '17 at 21:37
  • Alright. I set it as the correct answer but it's still not really the answer. I was asking if you could compare literal objects, not how to avoid it as if it were a "mistake" – imaxwill Apr 25 '17 at 21:43
  • Ah, I see you've edited your original post quite a bit, sorry about that. My recommendation would be to use a hash function to hash both objects and then compare the hashes. https://github.com/puleos/object-hash You could also try to stringify the entire object, strip out all whitespace and generate a checksum for each of the objects. If the checksums match, they are the same object. – Danny Bullis Apr 25 '17 at 21:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142663/discussion-between-maxwill-and-danny-bullis). – imaxwill Apr 25 '17 at 21:46