Created
March 3, 2023 13:24
-
-
Save ekaitz-zarraga/abba93e24f35e656361f353d229a20b7 to your computer and use it in GitHub Desktop.
a JavaScript Map that compares keys by equality and not by reference LUL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MyMap(){ | |
this.map = new Map(); | |
} | |
MyMap.prototype.set= function(k,v){ | |
this.map.set(JSON.stringify(k), v); | |
} | |
MyMap.prototype.get= function(k){ | |
return this.map.get(JSON.stringify(k)); | |
} | |
MyMap.prototype.has= function (k){ | |
this.map.has(JSON.stringify(k)); | |
} | |
MyMap.prototype.forEach= function(f){ | |
this.map.forEach((v,k,map)=>{ | |
f(v, JSON.parse(k), map); | |
}); | |
} | |
let m = new MyMap() | |
m.set([0,1],89); | |
console.log( m.get([0,1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment