r/javascript • u/Spatul8r • Jun 23 '26
AskJS [AskJS] I'm not a big fan of tuples here
I'm making a quick and dirty way to make dictionaries where many keys map to the same item, and instead of using a single hashable item as the key, it makes use of an array of hashable items to build the map. I don't like using tuples, but I don't think it would be valid to have an array as the key of a dictionary. I would love it if I could do that however. Is there a way to make a hashable array?
let map = MapFactory(
[
[[1,2,3], () => "1-3"],
[[4,5,6], () => "4-6"],
[[7,8,9], () => "7-9"]
]
);
console.log(map.get(1)()); // "1-3"
console.log(map.get(6)()); // "4-6"
function MapFactory(mapItems) {
const map = new Map();
for (let [keys, value] in mapItems) {
for(let key in keys) {
map.set(key, value);
}
}
return map;
}
12
Upvotes
6
u/ironykarl Jun 23 '26
This is sort of an xy problem, but ignoring that...
The key for a
Mapcan be any value (including objects/arrays), so in that context, values of those types are hashable6
u/prehensilemullet Jun 23 '26
By the way you have for..in loops in your example but they would need to be for..of loops to work the way you intended
5
u/prehensilemullet Jun 23 '26 edited Jun 23 '26
Even if you could use an array as a key and provide a custom function to compute the hash of an array, there would be no consistent way to insure that the individual elements (e.g. 1, 2, and 3 in this example) all have the same hash as the array itself, so looking up an individual number wouldn’t work.
If you want though, you could make a wrapper class whose constructor works like your example, and whose
setmethod accepts an array in place of the key, and creates entries for each element of the array as a key.4
u/cutmore_a Jun 23 '26
In the future I'm hoping that https://github.com/tc39/proposal-composites will make these patterns easier and more efficient
2
u/biskitpagla Jun 23 '26 edited Jun 23 '26
What's the actual use case? This doesn't seem like a multimap but you sound like you're looking for a multimap.
Due to arrays being reference types, you'll either have to use a nested map or stringify and then use a hash function from Crypto to get the key. In all cases you'll be doing tons of heap allocations, so it probably makes more sense to just engineer your program differently.
2
u/lanerdofchristian Jun 23 '26
Quick note on your specific example, you could probably skip the factory method:
4
u/ghost-engineer Jun 23 '26
i think you should focus on your engineering skills before trying to solve this red peg in a square hole problem.
1
u/ExtraTNT Jun 23 '26
https://github.com/Dierk/JS-Showcase/blob/main/lambda/lambda.js
Maybe a small hint for a tuple
1
u/HipHopHuman Jun 23 '26 edited Jun 24 '26
Kind of, but I don't think that'll help your use case.
It's done by using
JSON.stringify, which takes an optional callback function called areplacer. It will be called recursively for each level of nesting inside an object being stringified. The caveat is that you have to.sort()the object's keys so they're in a deterministic order. Something like this:The above will make it so you can do this:
But, as another commenter pointed out, that still doesn't solve the problem of associating individual numbers with the value any better than you've already done with your helper (though those
ins should be corrected toofs).Perhaps you could look into Map.groupBy instead?
1
u/Br1zz1713 Jun 24 '26 edited Jun 24 '26
Yeah reference equality in JS is annoying for this. Since map.get([1, 2]) is always undefined because it checks refs, the easiest way is to serialize the array.
Usually a simple `arr.join('|')` is enough. You can wrap it in a helper or write a quick class:
If the order of keys doesn't matter (like [1, 2] should match [2, 1]), just sort them first: `keys.slice().sort().join('|')`
1
u/CompetitionAsleep438 Jun 25 '26
h
1
u/CompetitionAsleep438 Jun 25 '26
honestly i just use a plain object until the tuple shape is actually fixed
0
u/microbiont Jun 23 '26 edited Jun 23 '26
As at least one other person commented, you can in fact use arrays and other objects as a keys in maps with no issue; they were designed for this purpose. The problem is that
[1,2,3] !== [1,2,3]because strict comparison is done by reference not by comparing primitive values within the object. Due to that, you can only get a value from a map using the original object as the key and not some look-a-like object or array.You can solve this by creating another Map that stores each of 1, 2, and 3 to the original
[1, 2, 3]array, then using that to look up in your other map! This last part is at least what ChatGPT tells me. /sEdit: People don't know what
\smeans anymore. The first paragraph is true and good knowledge though.-1
u/Spatul8r Jun 23 '26
Looks at these tuples go
Good enough for now. We need our rest so we can keep writing pretty code. I'm going to imagine it throwing runtime exceptions. And it'll make me sleep even better.