Files
hello-algo/en/codes/javascript/chapter_hashing/hash_map.js
Yudong Jin 14c314c08c Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
2026-10-31 03:35:51 +08:00

45 lines
1.2 KiB
JavaScript

/**
* File: hash_map.js
* Created Time: 2022-12-26
* Author: Justin (xiefahit@gmail.com)
*/
/* Driver code */
/* Initialize the hash table */
const map = new Map();
/* Insert operation */
// Insert the key-value pair (key, value) into the hash table
map.set(12836, '小哈');
map.set(15937, '小啰');
map.set(16750, '小算');
map.set(13276, '小法');
map.set(10583, '小鸭');
console.info('\nHash table after insertion:\nKey -> Value');
console.info(map);
/* Lookup operation */
// Look up key in the hash table to obtain value
let name = map.get(15937);
console.info('\nLook up student ID 15937 to obtain the name ' + name);
/* Remove operation */
// Remove the key-value pair (key, value) from the hash table
map.delete(10583);
console.info('\nHash table after removing 10583:\nKey -> Value');
console.info(map);
/* Traverse the hash table */
console.info('\nTraverse key-value pairs: Key->Value');
for (const [k, v] of map.entries()) {
console.info(k + ' -> ' + v);
}
console.info('\nTraverse keys only: Key');
for (const k of map.keys()) {
console.info(k);
}
console.info('\nTraverse values only: Value');
for (const v of map.values()) {
console.info(v);
}