Using Tables/Variables As Keys

Is it possible to use tables ([2, 5]) or variables/constants as keys?

“JavaScript automatically casts non-string keys to strings”

Seems impossible so a workaround with hand-typed

if (Key == [2, 2]) {
  return Dictionary.22Table;
}

would be necessary - and painful.

I’m not exactly sure I understand your question, but perhaps a Map is what you are looking for?

1 Like

Sorry for the late reply

I’ve just realized that it’s not practical (or possible) to use variables as dictionary keys, but what about tables? As far as I know, if I tried to assign {“password” : 123} as a dictionary key, it would result in an error, as only strings or numbers can be used as keys.

The result I expect is to be able to do this:

var Dictionary = {
  {"password" : 123} : "thisisavalue"
}

console.log(Dictionary[{"password" : 123}])

Did you look at the link I gave you about Maps?

From the documentation:

“A Map’s keys can be any value (including functions, objects, or any primitive).”

1 Like

As @bbsmooth is saying, keys are cast to strings so that cannot work.

Secondly, if you could use an object as a key, how would it work? An object is not the same as the values of the properties stored in the object, this “key”:

{"password" : 123}

Will not be the same as this “key”:

{"password" : 123}

So if you could do it, then trying to look up the value, like

Dictionary[{"password" : 123}]

Just won’t work because that key doesn’t match.

You can use objects as keys in Maps, but you still have exactly the same issue: it’s going to prevent you looking up by key.

What you are asking for is solved by something called a hash map, which is a map of hashed keys => values. JS objects are basically hash maps for example.

In this case, you have to provide some kind of hashing function that changes

{ "password": 123 }

Into (eg) a string when you store it. ie it gets hashed.

So for example on the object you want to use as a key,

  • that could be implemented as a class
  • that class could implement hash (to convert the object) and equals (to be able to look up & compare the object as a key) methods.

Then you could subclass Map, so that when you call set or whatever, it first checks if it can hash the object, then does that and sets the values. Then same thing for the other methods you require: looking up values, checking if a given “key” is in the map, etc.

1 Like

Thanks this was the solution I was looking for, my bad for being lazy

I’ve only started js, so I’ll probably learn about hashing sometime, but I think I understand the concept so thanks for going into a bit more detail :]

1 Like

Yeah it’s not important with respect to just starting out (and it’s not terribly important with respect to most day to stuff), but it’s a relatively important concept at the root of a load of stuff. Just a way to answer the question “how do I convert some data of arbitrary size to something of a fixed size”.

As it is, I get what you’re trying to do and why you want to do it, but what you’re going to need to do is shift your logic around so you don’t need to do it this way, I would say you need to look for a different approach

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.