Created
July 14, 2020 10:24
-
-
Save ger86/182722a69ca5c35a6a9aca8b6a6afd42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************** | |
* | |
* 🗺️🗺️🗺️ 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