0

Let's say I have this code:

let map = new Map();
map.set([2,2], 4);

I would expect for map.get([2,2]) to return 4, but for some reason it returns undefined

Omn
  • 39
  • 4
  • If you define `key = [2, 2]` and use `map.set(key)`, you can use `map.get(key)` to get the value back. See marked duplicate for more info. – 0stone0 Jun 28 '23 at 13:04
  • `let map = new Map(); let key = [2, 2]; map.set(key, 4);` – debugger Jun 28 '23 at 13:06
  • The basic problem is that Map keys are checked for *reference* equality and not *value* equality. So if the array you use to set the value and the one you use the test the value refer to the same place in memory, it will work as you hope. If you can't do that, then `Map` is not going to work, and you might look into a library that uses value equality. If, however, you're always using arrays of numbers for keys, then you could use `JSON.stringify` on them before setting or testing, and it should help. – Scott Sauyet Jun 28 '23 at 13:22

0 Answers0