Skip to content

Instantly share code, notes, and snippets.

@ger86
Created July 14, 2020 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ger86/182722a69ca5c35a6a9aca8b6a6afd42 to your computer and use it in GitHub Desktop.
Save ger86/182722a69ca5c35a6a9aca8b6a6afd42 to your computer and use it in GitHub Desktop.
/********************************************************************************
*
* 🗺️🗺️🗺️ Map 🗺️🗺️🗺️
* Performs better in scenarios involving frequent additions and
* removals of key-value pairs.
*
********************************************************************************/
// 1️⃣ Main methods
const map = new Map();
map.set('key', 'value');
console.log(map.get('key')); // value
console.log(map.has('key')); // true
console.log(map.size); // 1
// 2️⃣ Iterating.
// Keys are ordered by insertion time
map.set('aKey', 'this value appears last');
for (let [key, value] of map) {
console.log(`${key}: ${value}`);
}
// 3️⃣ It is possible using objects and functions as keys
const objectKey = {};
map.set(objectKey, 'This is a value with an object acting as key');
console.log(map.get(objectKey)); // This is a value with an object acting as key
function functionKey() {};
map.set(functionKey, 'This is a value with a function acting as key');
console.log(map.get(functionKey)); // This is a value with a function acting as key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment