Here is some example Javascript (ES6) code that does not do what one might intuitively imagine.
const exampleMap = new Map([[{a: 1}, 2]]);
console.log(exampleMap.get({a: 1}));
As it turns out, this prints undefined
. Why? The reasoning is covered in this StackOverflow answer. Per the MDN entry for Map, Map
uses ===
for key equality. And, per the MDN entry for ===
, Object
s are compared by reference equality.
That's all fine and good. It does exactly what the docs say it should. Unfortunately, what the above code is trying to do would be quite useful, even if it isn't the actual behavior per the spec.
How can I use Map
, or what should I use instead of Map
, to get a key-value lookup where the keys are compared by object deep-equality semantics?