You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 09-Data-Structure-Modern-Operators-and-Strings/README.md
+118
Original file line number
Diff line number
Diff line change
@@ -295,3 +295,121 @@ The syntax uses a question mark after the element we want to check conditionally
295
295
So the result in this operation will be `undefined` and the error we saw earlier will be avoided.
296
296
297
297
# 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 (constdayofObject.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 (constvalofObject.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
+
constentries=Object.entries(openingHours)
331
+
for(const [key, {openclose}] 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
+
constordersSet=newSet([
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 (constorderof 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.
0 commit comments