forked from mouredev/hello-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11-set.js
More file actions
73 lines (50 loc) · 1.63 KB
/
11-set.js
File metadata and controls
73 lines (50 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Clase 26 - Sets
*/
// Set es una colección de valores únicos
// Se diferencia de un array en que no admite duplicados
// No tiene índices
// No tiene propiedades length
// No tiene métodos push, pop, shift, unshift, etc.
// Declaración
let mySet = new Set(); // Set vacío
console.log(mySet);
// Inicialización
mySet = new Set([
"Juan",
"Antonio",
"jaf",
37,
true,
]); // Set con valores
console.log(mySet);
// Métodos comunes
// add y delete
mySet.add("https://juanprofesor.com"); // Añadir un valor
mySet.add("https://juanprofesor.com"); // No se añade porque ya existe
console.log(mySet);
mySet.delete("https://juanprofesor.com"); // Eliminar un valor
console.log(mySet);
console.log(mySet.delete("Juan")); // Eliminar un valor
console.log(mySet.delete(4)); // Eliminar un valor
console.log(mySet);
// has
console.log(mySet.has("Juan")); // Comprobar si existe un valor
console.log(mySet.has("Antonio")); // Comprobar si existe un valor
// size
console.log(mySet.size);
// Convertir un set a array
let myArray = Array.from(mySet); // Convertir un set a array
console.log(myArray);
// Convertir un array a set
myArray[5] = "jaf";
console.log("con el jaf duplicado: " + myArray);
mySet = new Set(myArray); // Convertir un array a set
console.log("con el jaf duplicado: " + mySet);
// No admite duplicados
console.log(mySet);