|
|
|
|
|
by sroussey
891 days ago
|
|
It’s not key comparison issue: const idx = [1,2]
const m = new Map
m.set(idx,"hi!")
console.log(m.get(idx)) // outputs "hi!" console.log(m.get([1,2]) // outputs undefined That last line has created a new array object, and Map is made to be fast so checks equality by reference. Ah, which is what you to be able to change. I guess you would want to pass a new map a comparator function, so that it does a deep equal. That would be faster than what you would have to do now: const idx2 = String([1,2])
m.set(idx2, "yo")
console.log(m.get(idx2)) // yo
console.log(m.get(String([1,2])) // yo |
|
Stringification is a very crude hack, and it doesn't even work in all cases - e.g. not if a part of your key still needs to be compared by reference. Say, it's a tuple of two object references, describing the association between those two objects.
Either way, the point is that this is really basic stuff that has been around in mainstream PLs for literally many decades now (you could do this in Smalltalk in 1980!).