Skip to content

Commit 2f0feb7

Browse files
committed
complete Maps section
1 parent 239f4b5 commit 2f0feb7

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

09-Data-Structure-Modern-Operators-and-Strings/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,71 @@ console.log(uniqueStaff);
413413
```
414414
415415
Sets are not intended to replace arrays at all but only for working with unique values.
416+
417+
# Maps
418+
419+
Map objects are a collection of key-value pairs. Similar to objects, their keys only occur once and it is unique in a collection.
420+
421+
The main difference is that `objects` only support string and `symbol` keys where as Maps supports any key type.
422+
423+
Maps are created by defining a new map and to add items to the map, we use the `set()` method, which takes in any key `name` and a value.
424+
425+
```js
426+
const rest = new Map();
427+
rest.set("name", "Classico Italiano"); // String as key
428+
rest.set(1, "Firenze, Italy"); // Number as key
429+
```
430+
431+
We can also chain the set method with other items.
432+
433+
```js
434+
rest
435+
.set(categories, [])
436+
.set("open", 11)
437+
.set("close", 23)
438+
.set(true, "We are open :D")
439+
.set(false, "We are closed :(");
440+
441+
console.log(rest);
442+
```
443+
444+
To get items inside a `Map`, we use the `get()` method, with the key name we want to get. The key name data type also matters.
445+
446+
```js
447+
console.log(rest.get(true));
448+
449+
const time = 21;
450+
console.log(rest.get(time > rest.get("open") && time < rest.get("close")));
451+
```
452+
453+
The rest also contains the `has()` method for checking for elements.
454+
455+
```js
456+
rest.has(categories); // True
457+
```
458+
459+
To remove elements from the Map, we use the delete method.
460+
461+
```js
462+
rest.delete(1);
463+
```
464+
465+
We can also use objects as Map keys
466+
467+
```js
468+
rest.set([1, 2], "Test");
469+
```
470+
471+
However, if we try to use the `get()` method on `[1, 2]`, it will not work because they are not in the same memory. In other to get the value `Test`, we store the element key in a variable and use that variable to get the element.
472+
473+
```js
474+
const arr = [1, 2];
475+
rest.set(arr, "Test");
476+
rest.get(rest.get(arr));
477+
```
478+
479+
We an also use the `rest` on `DOM` elements, which allows us to do advanced functionalities.
480+
481+
```js
482+
rest.get(document.querySelector("h1"));
483+
```

09-Data-Structure-Modern-Operators-and-Strings/script.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,54 @@ console.log(staffUnique);
401401
// Convert values to arrays
402402
const staffUniqueAr = [...new Set(staff)];
403403
console.log(staffUniqueAr);
404+
405+
// 🔸Maps🔸
406+
const rest = new Map();
407+
// Add elements
408+
rest.set("name", "Classico Italiano");
409+
rest.set(1, "Firenze, Italy");
410+
rest.set(2, "Lisbon, Portugal");
411+
console.log(rest);
412+
413+
rest
414+
.set(categories, [])
415+
.set("open", 11)
416+
.set("close", 23)
417+
.set(true, "We are open :D")
418+
.set(false, "We are closed :(");
419+
420+
console.log(rest);
421+
console.log(rest.get(true));
422+
423+
// Get elements
424+
const time = 21;
425+
console.log(rest.get(time > rest.get("open") && time < rest.get("close")));
426+
427+
// Check for elements
428+
console.log(rest.has(categories));
429+
430+
// Remove elements
431+
rest.delete(2);
432+
console.log(rest);
433+
434+
// Size of elements
435+
console.log(rest.size);
436+
437+
// Clear elements
438+
// rest.clear();
439+
// console.log(rest);
440+
441+
// We can also use object inside of maps
442+
rest.set([1, 2], "Test");
443+
console.log(rest);
444+
445+
// This will not work because it is not present in the same memory.
446+
console.log(rest.get([1, 2])); // Undefined
447+
448+
const s = [1, 2];
449+
rest.set(s, "Test");
450+
console.log(rest.get(s));
451+
452+
// DOM elements
453+
rest.set(document.querySelector("h1"), "Heading");
454+
console.log(rest);

0 commit comments

Comments
 (0)