Using Array objects as key for ES6 Map

Understand that ES2015 Map keys are compared (almost) as if with the === operator. Two array instances, even if they contain the same values, do not ever compare as === to each other.

Try this:

var a = new Map(), key = ['x', 'y'];
a.set(key, 1);
console.log(a.get(key));

Since the Map class is intended to be usable as a base class, you could implement a subclass with an overriding .get() function, maybe.

(The “almost” in the first sentence is to reflect the fact that Map key equality comparison is done via Object.is(), which doesn’t come up much in daily coding. It’s essentially a third variation on equality testing in JavaScript.)

Leave a Comment