Skip to content

Commit a834d95

Browse files
committed
complete maps iteration
1 parent 2f0feb7 commit a834d95

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

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

+61
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,64 @@ We an also use the `rest` on `DOM` elements, which allows us to do advanced func
481481
```js
482482
rest.get(document.querySelector("h1"));
483483
```
484+
485+
### Maps Iteration
486+
487+
Elements are added to a `Map` using the `set()` method. But this can be cumbersome if you're trying to add multiple elements at the same time. To fix that, we can create a new `map` using an array inside the `map`.
488+
489+
For example:
490+
491+
```js
492+
const question = new Map([
493+
["Question", "What is the best programming language in the world?"],
494+
[1, "C"],
495+
[2, "Java"],
496+
[3, "JavaScript"],
497+
["Correct", 3],
498+
[true, "Correct 🎉"],
499+
[false, "Try Again 💔"],
500+
]);
501+
```
502+
503+
This method makes it easier but in any case where we need to keep adding elements to the `Map` programtically, the `set()` method is best.
504+
505+
### Convert Objects to Map
506+
507+
```js
508+
const hoursMap = new Map(Object.entries(openingHours));
509+
console.log(hoursMap);
510+
```
511+
512+
```js
513+
// Quiz App
514+
console.log(question.get("Question"));
515+
516+
for (const [key, value] of question) {
517+
if (typeof key === "number") {
518+
console.log(`Answer ${key}: ${value}`);
519+
}
520+
}
521+
522+
const answer = Number(prompt("What is your answer?"));
523+
console.log(
524+
answer === question.get("correct")
525+
? `${answer} ${question.get(true)}`
526+
: `${answer} ${question.get(false)}`
527+
);
528+
```
529+
530+
Finally, it can be important to know how to convert a `Map` back to an array. To do that, simply use destructure the `Map`.
531+
532+
```js
533+
console.log(...question);
534+
```
535+
536+
Finally, `Maps` also contains the keywords for getting elements:
537+
538+
```js
539+
console.log(...question.entries());
540+
console.log(...question.keys());
541+
console.log(...question.values());
542+
```
543+
544+
# Summary: Which Data Structure to Use
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Write a funciton to convert a name into initials. This strictly takes two words with one space in between them.
2+
3+
The output should be two capital letters with a dot seperating them.
4+
5+
It should look like this:
6+
7+
Sam Harris => S.H
8+
patrick feeney => P.F
9+
*/
10+
11+
/*
12+
1. Create a function that prints a value to the console.
13+
2. The function will return the first letter of each word
14+
*/
15+
16+
function convertInitials(name) {
17+
const x = name.split(" ");
18+
console.log(x);
19+
20+
// console.log(`${name.charAt(0)}.${name.charAt(1)}`);
21+
// console.log(`${name[0].toUpperCase()}.${name[5].toUpperCase()}`);
22+
}
23+
24+
convertInitials("John Rambo");
25+
convertInitials("ashely bolanle");

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

+46-3
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,19 @@ console.log(ordersSet);
373373

374374
console.log(ordersSet.size);
375375

376-
// Has
376+
// Has in Set
377377
console.log(ordersSet.has("Bread"));
378378
console.log(ordersSet.has("Pizza"));
379379

380-
// Add & Delete
380+
// Add & Delete Set
381381
ordersSet.add("Garlic Bread");
382382
ordersSet.add("Garlic Bread");
383383
console.log(ordersSet);
384384

385385
ordersSet.delete("Risoto");
386386
console.log(ordersSet);
387387

388-
// Clear
388+
// Clear a set
389389
// ordersSet.clear();
390390

391391
for (const order of ordersSet) {
@@ -452,3 +452,46 @@ console.log(rest.get(s));
452452
// DOM elements
453453
rest.set(document.querySelector("h1"), "Heading");
454454
console.log(rest);
455+
456+
const dat = new Map();
457+
458+
// Map Iterations
459+
const question = new Map([
460+
["Question", "What is the best programming language in the world?"],
461+
[1, "C"],
462+
[2, "Java"],
463+
[3, "JavaScript"],
464+
["Correct", 3],
465+
[true, "correct 🎉"],
466+
[false, "Try Again 💔"],
467+
]);
468+
469+
console.log(question);
470+
471+
// Convert objects to Map
472+
const hoursMap = new Map(Object.entries(openingHours));
473+
console.log(hoursMap);
474+
475+
// Quiz App
476+
console.log(question.get("Question"));
477+
478+
for (const [key, value] of question) {
479+
if (typeof key === "number") {
480+
console.log(`Answer ${key}: ${value}`);
481+
}
482+
}
483+
484+
const answer = Number(prompt("What is your answer?"));
485+
console.log(
486+
answer === question.get("correct")
487+
? `${answer} ${question.get(true)}`
488+
: `${answer} ${question.get(false)}`
489+
);
490+
491+
// Convert Map to Array
492+
console.log(...question);
493+
494+
// Also contains the keywords in Objects
495+
console.log(...question.entries());
496+
console.log(...question.keys());
497+
console.log(...question.values());

0 commit comments

Comments
 (0)