Skip to content

Commit 239f4b5

Browse files
committed
feat: complete set
1 parent 430dba1 commit 239f4b5

File tree

3 files changed

+163
-3
lines changed

3 files changed

+163
-3
lines changed

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

+118
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,121 @@ The syntax uses a question mark after the element we want to check conditionally
295295
So the result in this operation will be `undefined` and the error we saw earlier will be avoided.
296296
297297
# Looping Objects, Object Keys, Values, and Entries
298+
299+
We can also loop Objects which are not iterables using the `For of` loop, though in an indirect way.
300+
301+
With objects, we can loop through property names(keys) or values.
302+
303+
### Property names(keys)
304+
305+
```js
306+
for (const day of Object.keys(openingHours)) {
307+
console.log(day);
308+
}
309+
```
310+
311+
This will fetch all the key names (thu, fri, sat) of the openingHours object.
312+
313+
We can also get property values using a similar fashion.
314+
315+
### Property values
316+
317+
```js
318+
for (const val of Object.values(openingHours)) {
319+
console.log(val);
320+
}
321+
```
322+
323+
But in other to get the full content of the array, we need a property called `entries`. Which is basically a method that returns both `keys` and `values` together.
324+
325+
In objects it works a bit differently, as we don't call it as a method.
326+
327+
### Entire Object
328+
329+
```js
330+
const entries = Object.entries(openingHours)
331+
for(const [key, {open close}] of entries) {
332+
console.log(`On ${key}, we open at ${open} and close at ${close}`)
333+
}
334+
```
335+
336+
# Sets
337+
338+
In the past, we only had two data structures: Arrays and Objects, but in ES6, two other data structures were added to the list: Sets and Maps.
339+
340+
A set is a collection of unique values. Which means that a set can never have any duplicates.
341+
342+
```js
343+
const ordersSet = new Set([
344+
"Pasta",
345+
"Pizza",
346+
"Pizza",
347+
"Risoto",
348+
"Pasta",
349+
"Pizza",
350+
]);
351+
console.log(ordersSet);
352+
```
353+
354+
The result of this will be `'Pasta', 'Pizza', and 'Risoto'`. It will eliminate all duplicates. You can also pass in a string.
355+
356+
## How to work with sets
357+
358+
- Size
359+
You can get the number of unique values in a set using the `size`
360+
property. This is similar to the `.length` method in arrays
361+
362+
```js
363+
console.log(ordersSize.size);
364+
```
365+
366+
- If an element is in a set
367+
With the `has` property, you can check if a value exists in a set. This property is similar to the `includes()` method in Arrays
368+
369+
```js
370+
console.log(ordersSize.has("Bread"));
371+
```
372+
373+
- Add and delete elements in a set
374+
375+
```js
376+
ordersSet.add("Garlic Bread");
377+
ordersSet.delete("Garlic Bread");
378+
```
379+
380+
You can't get values from a set. If you need to get a list of values, we still use Arrays.
381+
382+
- Clear
383+
Use this property to clear all the elements inside a set.
384+
385+
```js
386+
ordersSet.clear();
387+
```
388+
389+
Since sets are iterables, we can also loop over them.
390+
391+
```js
392+
for (const order of ordersSet) {
393+
console.log(order);
394+
}
395+
```
396+
397+
## Use case for Sets
398+
399+
The main use case for sets is to remove duplicate values in arrays. For example, let's say we need to get the unique values in an array and avoid all duplicates, here's how we can do that using a set.
400+
401+
```js
402+
const staff = ["Waiter", "Chef", "Waiter", "Manager", "Chef", "Waiter"];
403+
404+
const staffUnique = new Set(staff);
405+
console.log(staffUnique);
406+
```
407+
408+
The result will still a set. To convert it into an array, we use the spread operator
409+
410+
```js
411+
const uniqueStaff = [...new Set(staff)];
412+
console.log(uniqueStaff);
413+
```
414+
415+
Sets are not intended to replace arrays at all but only for working with unique values.

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

+44-2
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,48 @@ const entries = Object.entries(openingHours);
356356
console.log(entries);
357357

358358
// [key, value]
359-
for (const [q, { open, close }] of entries) {
360-
console.log(`On ${q} we open at ${open} and close at ${close}`);
359+
for (const [day, { open, close }] of entries) {
360+
console.log(`On ${day} we open at ${open} and close at ${close}`);
361361
}
362+
363+
// 🔸Sets🔸
364+
const ordersSet = new Set([
365+
"Pasta",
366+
"Pizza",
367+
"Pizza",
368+
"Risoto",
369+
"Pasta",
370+
"Pizza",
371+
]);
372+
console.log(ordersSet);
373+
374+
console.log(ordersSet.size);
375+
376+
// Has
377+
console.log(ordersSet.has("Bread"));
378+
console.log(ordersSet.has("Pizza"));
379+
380+
// Add & Delete
381+
ordersSet.add("Garlic Bread");
382+
ordersSet.add("Garlic Bread");
383+
console.log(ordersSet);
384+
385+
ordersSet.delete("Risoto");
386+
console.log(ordersSet);
387+
388+
// Clear
389+
// ordersSet.clear();
390+
391+
for (const order of ordersSet) {
392+
console.log(order);
393+
}
394+
395+
// Use Case
396+
const staff = ["Waiter", "Chef", "Waiter", "Manager", "Chef", "Waiter"];
397+
398+
const staffUnique = new Set(staff);
399+
console.log(staffUnique);
400+
401+
// Convert values to arrays
402+
const staffUniqueAr = [...new Set(staff)];
403+
console.log(staffUniqueAr);

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
</head>
1111
<body>
1212
<h1>Console.log</h1>
13-
<script src="09-Data-Structure-Modern-Operators-and-Strings/challenge.js"></script>
13+
<script src="09-Data-Structure-Modern-Operators-and-Strings/script.js"></script>
1414
</body>
1515
</html>

0 commit comments

Comments
 (0)