Skip to content

Latest commit

 

History

History
130 lines (90 loc) · 2.88 KB

18-map-solution.md

File metadata and controls

130 lines (90 loc) · 2.88 KB

Exericse 18 Solutions

Question 1: Create a new Map and add key-value pairs to it

const myMap = new Map();
myMap.set('name', 'John');
myMap.set('age', 30);

Question 2: Get the value of a specific key in a Map

const age = myMap.get('age'); // Returns 30

Question 3: Check if a key exists in a Map

const hasName = myMap.has('name'); // Returns true
const hasEmail = myMap.has('email'); // Returns false

Question 4: Iterate over the keys of a Map using a for...of loop

for (const key of myMap.keys()) {
    console.log(key);
}

Question 5: Iterate over the values of a Map using a for...of loop

for (const value of myMap.values()) {
    console.log(value);
}

Question 6: Iterate over the key-value pairs of a Map using a for...of loop

for (const [key, value] of myMap) {
    console.log(key, value);
}

Question 7: Convert a Map to an array of key-value pairs

const arrayFromMap = Array.from(myMap);

Question 8: Convert an array of key-value pairs to a Map

const array = [['color', 'blue'], ['size', 'medium']];
const mapFromArray = new Map(array);

Question 9: Use the forEach() method to iterate over the key-value pairs of a Map

myMap.forEach((value, key) => {
    console.log(key, value);
});

Question 10: Use the Map keys() method to get an iterator for the keys of a Map

const keysIterator = myMap.keys();

Question 11: Use the Map values() method to get an iterator for the values of a Map

const valuesIterator = myMap.values();

Question 12: Use the Map entries() method to get an iterator for the key-value pairs of a Map

const entriesIterator = myMap.entries();

Question 13: Use the Map has() method to check if a Map contains a key

const hasEmail = myMap.has('email'); // Returns false

Question 14:

Use the Map get() method to get the value associated with a key in a Map

Question 15: Use the Map set() method to add a new key-value pair to a Map

myMap.set('email', 'john@example.com');

Question 16: Use the Map delete() method to remove a key-value pair from a Map

myMap.delete('age');

Question 17: Use the Map clear() method to remove all key-value pairs from a Map

myMap.clear();

Question 18: Use the Object.fromEntries() method to create a new object from a Map

const objFromMap = Object.fromEntries(myMap);

Question 19: Use the Map constructor to create a new Map from an array of key-value pairs

const keyValuesArray = [['color', 'red'], ['size', 'large']];
const newMap = new Map(keyValuesArray);