-3
const dict = new Map([
   ['a', '1'],
   ['b', '2']
]

We know that dict.get(key) returns the value but what if I want to get the key of value?

This question was never asked. I searched for 3 hours on stack overflow they all create sets or something I don't need

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • 1
    https://stackoverflow.com/questions/9907419/how-to-get-a-key-in-a-javascript-object-by-its-value. Afaik, there is no standard method for this requirement, you have to build your own. – Wen Seng Tee Feb 06 '23 at 06:58
  • @WenSengTee it's not the same format check his map it's a json object – looooltroll Feb 06 '23 at 07:00
  • Do you want a unique key or all the keys matching a value? – Phil Feb 06 '23 at 07:01
  • @Phil Yes it's a unique key – looooltroll Feb 06 '23 at 07:03
  • What if there are multiple keys with the same value? Which key should be matched? For example... `dict.set('c', '1')` – Phil Feb 06 '23 at 07:03
  • @Phil Actually I try to encrypt text by using that map so I make sure it's a unique key if it's not then that would mess up everything – looooltroll Feb 06 '23 at 07:04
  • @looooltroll sorry, I'm not following. Sounds like there's a lot of information missing from your question. There is no guarantee in a `Map` that values are unique so what should happen in case of a conflict? – Phil Feb 06 '23 at 07:05
  • It's not clear to me how getting a key from a map by its value is different from getting a key from a map by its value. Please elaborate what the difference is because I don't see it. – VLAZ Feb 06 '23 at 07:14
  • 1
    1. There are no sets at all in any of the answers. I don't know what you mean by "*They use Sets*" 2. [This answer](https://stackoverflow.com/a/60384362) already shows the same as the answer on this question - creating a reverse Map. Why didn't that work for you but the answer here did? Considering the code is basically the same - get a map, flip the keys and values, create a new map out of it. – VLAZ Feb 06 '23 at 07:20
  • @looooltroll you sure you're looking at the right link? Your question has been closed as a duplicate of [this one](https://stackoverflow.com/q/47135661/283366). There is not a single use of `Set` in any of the answers – Phil Feb 06 '23 at 07:26
  • 7
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](https://meta.stackexchange.com/q/5221) – Adriaan Feb 06 '23 at 07:26

1 Answers1

1

Generally if you have two-way data that you need to access, you either have two maps if a key could be the same as a value, otherwise just a single map.

When you add something to one of these maps, make sure to add it to the other, with the keys/values swapped.

const dict1 = new Map([
   ['a', '1'],
   ['b', '2']
]);

// automatically make the second, inverted map
const dict2 = new Map([...dict1.entries()].map(([k, v]) => [v, k]));

console.log(dict2.get('1'));

If you wanted to, you could also make your own class that uses Maps under the hood to make the interface easier to work with.

Samathingamajig
  • 11,839
  • 3
  • 12
  • 34