My question is instead of inheriting the array prototype
Is there some way we can manipulate associative arrays to use them as Hashmaps with functions like getKey,getValue,ContainsKey,ContainsValue?
1 Answer
You can use the Map class that Javascript provides:
const map = new Map();
map.set("key", 23);
console.log(map.get("key")); // 23
console.log(map.has("key")); // true
An alternative that works as well and is kinda a basic concept of Javascript are normal Objects since they behave like maps. Eventhough you can use normal objects, I recommend using Map.
const obj = {
"key": 23
}
console.log(obj["key"]); // 23
obj["another key"] = 420;
console.log(obj["another key"]); // 420
// and the ugly version of Map.has:
console.log(obj["key"] !== undefined); // true
Map.