Let say we have a map in JS as below.
const someMap = new Map();
someMap.set(["Jack", "Mathematics"], "Fee=₹120000");
someMap.set(["Alyne", "Science"], "Fee=₹90000");
// Going to be undefined because the lists are compared by ===
console.log(someMap.get(["Jack", "Mathematics"]));
This prevents us from checking for keys in a map dynamically because of ===
comparison. One way I can think of is to convert it to a string like "Jack, Mathematics"
and then use it as a key. But this doesn't look perfect.
What is the best way to represent keys which are lists?
By best way, I mean a way where we preserve the list structure of the key. Let say we are converting the list ["adog", "isananimal"]
to a string "adog,isananimal"
and there is another list ["adog,isananimal"]
which converts to the same string "adog,isananimal"
which would create a confusion while retrieving the values.