0

I need to operate on a coordinate object like this:

type Pos = [number, number];

I want to make a Map from coordinates to string:

type Board = Map<Pos, string>

let a1 = [1,1]

let board = new Map([a1, 'a1']);

Now I can access a1 like board.get(a1).

But what if I generate a1 like this [1,1] and look it up:

board.has([1,1]) // false, I want this to be true
eguneys
  • 6,028
  • 7
  • 31
  • 63
  • Not so easily. You'll need to (1) retrieve all keys from the map (2) iterate through them to find which one matches the indicies you're looking for by using one of the linked methods – CertainPerformance Apr 17 '21 at 23:57
  • So it's not a good idea to use array as keys in javascript? – eguneys Apr 17 '21 at 23:57
  • 1
    You can if you want, but if you won't have a reference to the created arrays later to look up values, it's probably not a good idea, since the logic you'll require will take quite a few lines for something that *should* be trivial. Consider a different data structure, like keys of `'1_1'` instead – CertainPerformance Apr 17 '21 at 23:59
  • But I need to operate on the individual numbers also test them for reference equality like case classes in scala – eguneys Apr 18 '21 at 00:03

0 Answers0