I have a class that has a static dictionary that maps ids to names
class Foo {
static idToName = {
2: "John",
8: "Peter"
}
}
Now, I also need to be able to get the id given a name, so I need the inverse dictionary
class Foo {
static idToName = {
2: "John",
8: "Peter"
}
static nameToId = {
"John": 2,
"Peter": 8
}
}
Can I avoid hardcoding this inverse dictionary and generate it with code?
At the end I need to be able to do
Foo.idToName(2) -> "John"
Foo.nameToId("John") -> 2
Is that possible?